简体   繁体   English

Python:多线程还是循环?

[英]Python: Multi-threading or Loops?

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

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) (闲置X秒钟后熄灭灯)

In your pseudocode you currently have an infinite recursion, which you definitely don't want. 在您的伪代码中,您当前具有无限递归,这绝对是您不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM