基本系统应该可以在没有任何多线程的情况下实现.但是,您需要一些事件处理才能有定时事件.(闲置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 roomdef MotionSenseLeave(x): x=0 if x==0: print("3. Someone left") #Wait for someone to leave by checking condition LightSense() else: x==0 return xdef 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 xMotionSenseEnter(x) #begin operationBasically, 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 receiveloop, polling information from the sensorMultithreading 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:多线程还是循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 06:33
查看更多