How do I run two indefinite loops simultaneously, while also changing variables within them?
<p>I'm trying to write a script in Python that records a stream of an IP camera in realtime. It only keeps about a minute worth of recording, constantly overwriting the same files. Whenever an external sensor is triggered I want a variable to be set (event variable) which merges the recording with an extra 30 seconds it records after the sensor is triggered. The combined 90 seconds are then saved as the date and time for later review.</p>

<p>The idea was to have 2 indefinite while loops, the first containing both the real time recording and the event. The second one would constantly read input and activate the 'event' function. Initially I though I could just have a software version of the hardware interrupt I've learned before, though after some research it seems that's only for exceptions. I'm currently using TkInter, simulating the external input with keypresses.</p>

<p>When I tried it out the input wouldn't cause the event to be triggered. So my question is: How do I run the two indefinite loops simultaneously, while also having the input loop change variables in the main loop so that the 'event' can actually occur in real-time? </p>

<p>Since I'm using ffmpeg to record the stream, once the command is called to record it can't be stopped, but I want the event variable to be changed as soon as possible.</p>

<p>I've looked at several similar questions regarding multiple loops, and have tried multiprocessing(though this only seems to be used for performance, which is not that important here), making two separate files(not sure how to have them work together) and lastly, threads. None of these seem to work in my situation as I can't get them running in the way that I want.</p>

<p>Here is my latest attempt, using threads:</p>

<pre><code>i = 0
event = False
aboutToQuit = False
someVar = 'Event Deactivated'
lastVar = False

def process(frame):
    print "Thread"
    i = 0    
    frame = frame 
    frame.bind("&lt;space&gt;", switch)
    frame.bind("&lt;Escape&gt;", exit) 
    frame.pack()
    frame.focus_set()

def switch(eventl):
    print(['Activating','Deactivating'][event])
    event = eventl
    event = not(event)

def exit(eventl):
    print'Exiting Application'
    global aboutToQuit
    #aboutToQuit = True
    root.destroy()

print("the imported file is", tkinter.__file__)
def GetTime(): #function opens a script which saves the final merged file as date and time.
    time = datetime.datetime.now()
    subprocess.call("./GetTime.sh", shell = True)
    return (time)

def main(root):
    global event, aboutToQuit, someVar,lastVar      
    while (not aboutToQuit):
        root.update() # always process new events

        if event == False:
            someVar = 'Event Deactivated'
            subprocess.call(Last30S_Command) #records last 30 seconds overwriting itself.
            print "Merge now"
            subprocess.call(Merge_Command) #merges last 30 seconds with the old 30 seconds       
            print "Shift now"
            subprocess.call(Shift_Command) #copies the last30s recording to the old 30 seconds, overwriting it.
            if lastVar == True: #Triggers only when lastVar state changes
                print someVar
                lastVar = False
            time.sleep(.1)

        if event == True: 
             someVar = 'Event Activated'
            print"Record Event"
            subprocess.call(EventRecord_Command) #records 30 seconds after event is triggered.
            print"EventMerge Now"
            subprocess.call(EventMerge_Command) # merges the 1 minute recording of Merge_Command with 30 seconds of EventRecord_Command
            if lastVar == False:
                print someVar
                lastVar = True
            time.sleep(.1)
            GetTime() #Saves 90 seconds of EventMerge_Command as date and time.
            subprocess.call(EventShift_Command) #Copies EventRecord file to the old 30 second recording, overwriting it

        if aboutToQuit:
           break


if __name__ == "__main__":
    root = Tk() 
    frame = Frame(root, width=100, height=100)   
#   maintthread = threading.Thread(target=main(root))
#   inputthread = threading.Thread(target=process(frame))
#   inputthread.daemon = True
#   inputthread.start()
#   maintthread.daemon = True
#   maintthread.start()
#   maintthread.join()
    Process(target=process(frame)).start()
    Process(target=main(root)).start()
    print "MainLoop"
</code></pre>

<p>Two processes won't share data, so each process will contain a copy of those global variables, which won't work for you.</p>

<p>The best bet is either threading or co-routines(gevent). I"m assuming your logic is -> record 30 seconds, check if event triggered, if so, record another 30 seconds and merge. i.e. I"m assuming you don't need to stop recording as soon as the event is triggered.</p>

python multithreading ffmpeg