I am writing an application that listens to sound events (using messages sent using Open Sound Control), and then pauses or resumes the program based on these events. My structure works most of the time, but always fires in the main loop, so I assume this is a thread issue. Here is a general, simplified version of what I'm talking about:
import time, threading class Loop(): aborted = False def __init__(self): message = threading.Thread(target=self.message, args=((0),)) message.start() loop = threading.Thread(target=self.loop) loop.start() def message(self,val): if val > 1: if not self.aborted: self.aborted = True
Of course, this simple code works fine, so it clearly does not fully illustrate my real code, but you get this idea. What is not included here is also due to the fact that pause and resume on each loop (by setting aborted = True / False) leads to some socket connection, which also includes streams.
What always happens in my code is that the main loop does not always work when it stops after an audio event. He will work for several events, but in the end he simply does not respond.
Any suggestions on how to structure such a relationship between threads?
UPDATE:
OK, I think I did it. there is a modification here that seems to work. There is a listener thread that periodically places a value in a Queue object. there is a control thread that continues to check the queue, looking for the value, and as soon as she sees that it sets the logical value to the opposite state. that a boolean controls whether the loop continues or waits for the loop.
I'm not quite sure what the q.task_done () function does here.
import time, threading import Queue q = Queue.Queue(maxsize = 0) class Loop(): aborted = False def __init__(self): checker = threading.Thread(target=self.checker) checker.setDaemon(True) checker.start() loop = threading.Thread(target=self.loop) loop.start() def checker(self): while True: if q.get() == 2: q.task_done() if not self.aborted: self.aborted = True else: self.aborted = False def loop(self): cnt = 0 while cnt < 40: if self.aborted: while self.aborted: print "waiting" time.sleep(.1) print cnt cnt += 1 time.sleep(.1) class fakeListener(): def __init__(self): listener = threading.Thread(target=self.listener) listener.setDaemon(True) listener.start() def listener(self): while True: q.put(2) time.sleep(1) if __name__ == '__main__':