Interact with long running python process
Question: I have a long running python process running headless on a raspberrypi
(controlling a garden) like so:

    
    
    from time import sleep
    
    def run_garden():
        while 1:
            /* do work */
            sleep(60)
    
    if __name__ == "__main__":
        run_garden()
    

The 60 second sleep period is plenty of time for any changes happening in my
garden (humidity, air temp, turn on pump, turn off fan etc), BUT what if i
want to manually override these things?

Currently, in my `/* do work */` loop, i first call out to another server
where I keep config variables, and I can update those config variables via a
web console, but it lacks any sort of real time feel, because it relies on the
60 second loop (e.g. you might update the web console, and then wait 45
seconds for the desired effect to take effect)

The raspberryPi running `run_garden()` is dedicated to the garden and it is
basically the only thing taking up resources. So i know i have room to do
something, I just dont know what.

Once the loop picks up the fact that a config var has been updated, the loop
could then do exponential backoff to keep checking for interaction, rather
than wait 60 seconds, but it just doesnt feel like that is a whole lot better.

Is there a better way to basically jump into this long running process?


Answer: Why not use an event based loop instead of sleeping for a certain amount of
time.

That way your loop will only run when a change is detected, and it will always
run when a change is detected (which is the point of your question?).

You can do such a thing by using:  
[python event objects](http://docs.python.org/2/library/threading.html#event-
objects)

Just wait for one or all of your event objects to be triggered and run the
loop. You can also wait for X events to be done, etc, depending if you expect
one variable to be updated a lot.

Or even a system like: [broadcasting
events](http://stackoverflow.com/questions/1092531/event-system-in-python)

