Python:多线程还是循环? [英] Python: Multi-threading or Loops?

查看:60
本文介绍了Python:多线程还是循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用多线程的想法在光强度传感器和运动传感器之间进行信息共享是否可行.我正在做一个项目,以使房间环境中的照明质量自动化.(我是编程的初学者,希望得到您的反馈!)

我的开始方式只是测试具有简单数值条件的代码并立即打印语句.下面是该项目的模拟代码.

  x = 1 #set只是说一些正在进入房间定义MotionSenseLeave(x):x = 0如果x == 0:print("3.有人离开")#等待某人通过检查条件离开LightSense()别的:x == 0返回xdef LightSense():#开灯如果x == 1:#并且灯光质量高于/低于某个值#print("2.Adjust Light")#测量光的质量MotionSenseLeave(x)elif x == 0:打印(停止操作,离开的人")MotionSenseEnter(x)elif x == 1:#和光接近值打印(光质量足够,无需更改")MotionSenseLeave(x)定义MotionSenseEnter(x):而x!= 1:如果x == 1:print("1.Someone here")#等待有人进来LightSense()别的:x == 0返回xMotionSenseEnter(x)#开始操作 

基本上,该操作从MotionSenseEnter()开始,并等待输入条件或x = 1(在这种情况下,我只是将其开头设置为1).如果有人进入,然后转到LightSense(),打开灯,然后开始案件,在每个案件之后,我都运行MotionSenseEnter()或MotionSenseLeave()函数以查看该人是否进入/离开以打开/关闭系统,然后从头开始重新启动它,直到检测到条目为止.

我想知道多线程化这些进程是否会更有效.如果是这样,我想我必须先检查是否有人进入房间,然后对离开条件和光线监视条件进行多线程处理以继续操作或关闭它,然后等待再次进入.如果有人对此有任何意见,欢迎所有反馈.再说一次,我是编程的初学者,我欣赏所有想法,但是简单的想法对我来说最有效!

解决方案

这取决于您如何从传感器获取信息以及调整光的含义.

您正在使用控制系统,这通常意味着您想绘制并实现一个状态机进行操作.您想要状态为熄灯",点亮",并且事件为检测到运动",运动停止"-也许还有一些计时器的其他状态,这样即使有人站着也不会闪烁灯./p>

根据您的输入,您将必须:

  • 从收到的推送事件中进行驱动
  • 循环,从传感器轮询信息

对于其中任何一个都不需要多线程.可能有用的是驱动灯光:如果您希望它们渐变而不是立即打开,则可能需要一个单独的线程来随时间调整灯光以达到状态机所期望的水平.

基本系统应该可以在没有任何多线程的情况下实现.但是,您需要一些事件处理才能有定时事件.(闲置X秒钟后熄灭灯)

在您的伪代码中,您目前具有无限递归,这绝对是您不希望的.

I was wondering if it would be feasible to use the idea of multi-threading to perform information sharing between a light intensity sensor and a motion sensor. I'm working on a project to automate lighting quality in a room environment. (I'm a beginner at programming and would appreciate any feedback!)

The way I'm starting is just to test out code with simple numerical conditions and print statements for now. Below is the simulated code for the project.

x=1 #set simply to say some is enters room

def MotionSenseLeave(x):

    x=0
    if x==0:
        print("3. Someone left")           #Wait for someone to leave by checking condition
        LightSense()
    else:
        x==0
    return x 

def LightSense():

    #Turn on lights
    if x==1:               #and light quality is above/below a value#
        print("2. Adjust Light")    #Measure Light Quality
        MotionSenseLeave(x)
    elif x==0: 
        print("Stop operation, the person left")
        MotionSenseEnter(x)
    elif x==1:          #and light is close to value
        print("Light quality sufficent, no changes needed")
        MotionSenseLeave(x)

def MotionSenseEnter(x):

    while x!=1:
        if x==1:
            print("1. Someone here")           #Wait for someone to come in
            LightSense()
        else:
            x==0
    return x   

MotionSenseEnter(x)                           #begin operation

Basically, the operation begins with MotionSenseEnter() and waits for the condition of entry or x=1 (in this case I just set it to 1 in the beginning). If someone enters, then go to LightSense(), turn on lights, then the cases begin and after each case I run the MotionSenseEnter() or MotionSenseLeave() function to see if that person enters/leaves to shut on/off the system and restart it from the beginning until entry is detected.

I was wondering if it would be more efficient to multi-thread these processes instead. If it is, I think I would have to first check to see if someone entered the room, then multi-thread the leave condition and light monitoring condition to continue the operation or close it and await for entry again. If anyone has any opinion on this, all feedback would be welcomed. Again, I'm a beginner at programming, I appreciate all ideas, but simple ones would work best for me!

解决方案

It depends how you're getting the information from the sensor and what does it mean to adjust the light.

You're doing a control system which usually means you want to draw and implement a state machine for your operations. You want states like "lights off", "lights on", and your events are "motion detected", "motion stopped" - maybe with some extra states for a timer so you don't flicker the lights if someone stands still.

Depending on your inputs, you will have to either:

  • drive it from pushed events which you receive
  • loop, polling information from the sensor

Multithreading shouldn't be necessary for either one of those. What it may be useful for is driving the lights: if you want them to ramp up rather than just turn on instantly, you may want a separate thread which adjust the light over time to get to the level expected from the state machine.

The basic system should be possible to implement without any multithreading. You'll need some event handling to have a timed event though. (turn lights off after X seconds of inactivity)

In your pseudocode you currently have an infinite recursion, which you definitely don't want.

这篇关于Python:多线程还是循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆