mardi 27 février 2018

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). 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!

Aucun commentaire:

Enregistrer un commentaire