multithreading - Python threading.Event() - Ensuring all waiting threads wake up on event.set() -


I have lots of threads waiting on an event, do some action, then wait for the event again . Another thread will trigger the event, when it is appropriate.

I can not understand a way to ensure that every wait thread triggers once upon event once set. I have established the trigger thread at the moment, go to sleep for a while, then clear it. Unfortunately, this often leads to the thread waiting for catching the set object, or not at all.

I can not just generate the response threads to run the trigger thread once because they are the answers to the requests made from the other place.

In short: In the Python, how does a thread set an event to me and ensures that every wait thread works fine once before correcting the event?

Update:

I've tried setting it up using lock and queue, but it does not work what I have :

  #GLOBALS - Threads are being used to wait OnEvent = Q. QEI MainEvent = ThreadingEvent () MainEvent.clear () # Sure it is necessary, but I thought I'd be safe mainLock = threading.Lock () def waitCall (): mainLock.acquire () waitingOnEvent.put ("Waiting") mainLock.release () MainEvent.wait () waitingOnEvent.get (False) waitingOnEvent.task_done () # Return to content triggerCall (): mainLock.acquire () itemsinq = waitingOnEvent.qsize () MainEvent.set ( ) WaitingOnEvent.join () MainEvent.clear () mainLock.release () Return  

For the first time, how many calls are waiting to encack correctly, but to make calls for Evl first wait bag it will make then, Aitmsink is always 1, and waiting threads are changing; Every time a trigger call occurs, then goes through the next.

Update 2 It appears that only one of the events. The weight (thread) rises , and yet queue.join () is working. This suggests to me that a waiting thread wakes up, grabs it with the queue, and calls task_done (), and that single receives () / task_done () empties the queue in some way and allows to join () . The trigger thread completes the joining (), clears the incident, and thus prevents others waiting threads from going to the place after empty / expired as Qi records why enter only after a received / work_on call Will be done, though?

Only one person wakes up, even if I have removed the queue .get () and que.task_done () and hang the trigger so that it can not clear the event.

You do not need an incident, and you do not need both a lock and a line You have only one line.

Call queue.put to leave the message without waiting for it to be sent or processed.

call

  import threading import queue active_ques = [] class worker (threading thread): def __init __ (self)): Threading.Thread .__ init __ (self) self.mailbox = queue.Queue () active_queues.append (self.mailbox) def run (self): while true: data = self.mailbox.get () if data == 'shutdown': print itself, 'Shutdown' returns the print itself, 'a message has been received:', data def stop (self): active_queues.remove (self.mailbox) self.mailbox.put ("shutdown") self.join () DRF broadcast_event (Data): for active q in q: q.put (data) t1 = functional (T1). T2.start () t2.start () t2.start () broadcast_event ("first event") broadcast_event ("sec ond event") broadcast_event ("shutdown") t1.stop () t2.stop ()  

Message string is not required; They can be any python object.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -