1

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!

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • Python in general is not good for multithreaded applications. See [here](https://stackoverflow.com/questions/20939299/does-python-support-multithreading-can-it-speed-up-execution-time) – Sanchit Anand Feb 27 '18 at 22:56
  • @SanchitAnand This is not a situation when you need great performance. The extra value comes from interleaved execution and doing something while IO operations are happening. Python is just fine with that. – viraptor Feb 27 '18 at 22:57
  • There's confusion about threads in Python 'cause the most common interpreters don't actually execute them in parallel. You can take full advantage of multiple CPU cores and communicate easily across _processes_ (not threads within one process) using the `multiprocessing` module in Python. It then becomes similar in usage to the multithreading you're maybe used to in C or Rust. Though I would recommend using a more suitable language than Python for performance-aware multithreaded computing. I'd say it's very clunky and unpredictable in that area. – sudo Feb 27 '18 at 23:51

1 Answers1

2

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.

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • Great answer. The finite state machine makes things a lot easier in the end than one would think. – sudo Feb 27 '18 at 23:47