python - How to know if threading.Condition.wait(timeout) has timed out or has been notified? -


i'm developing application threads, each 1 running infinite loop time sleep. want finish threads once main 1 has finished, here example:

def main():      display_res_stop = threading.condition()     display_result_t = threading.thread(target=sample_t, args=(display_res_stop, ))     display_result_t.start()      time.sleep(4)      display_res_stop.acquire()     display_res_stop.notify()     display_res_stop.release()   def sample_t(stop_cond):     stop_cond.acquire()      while true:         print 5         c = stop_cond.wait(10)      stop_cond.release()  if __name__ == '__main__':     main() 

the problem solution don't know if condition.wait has finished because timeout or because has been notified. in second case while loop should finish.

at first doing time.sleep(t) , using threading events application has wait until t on threads has passed.

i'm thinking mixed solution using threading.condition , event don't know if it's nicest thing (condition 'sleep' , event replace while true).

after simple, focusing on wrong thing: needed sleep stopped event , thats event.wait(t) does. problem then, can solved events.

def main():     stop_ev = threading.event()     sample_t = threading.thread(target=sample_t, args=(stop_ev, ))     sample_t.start()      # other stuff here, sleep dummy     time.sleep(14)      stop_ev.set()      print 'end reached.'  def sample_t(stop_ev):     while not stop_event.is_set()::         print 'thread iteration'         stop_ev.wait(10)  if __name__ == '__main__':     main() 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -