'Python multiprocessing: Event.wait() is returning False when another process calls Event.set() even before timeout
According to the documentation on Event.wait(): "This method returns True if and only if the internal flag has been set to true, either before the wait call or after the wait starts, so it will always return True except if a timeout is given and the operation times out"
But in one of my use-case, the Event.wait() method is returning False on calling set from another process before the timeout.
Below are the code snippets with description
I have a main process which is creating a subprocess and waiting for the subprocess to call the Event.set()
Main process:
event = multiprocessing.Event()
process = multiprocessing.Process(name='worker1', target=worker_main, args=(event,))
process.start()
event.clear()
start_status = event.wait(timeout=30)
if start_status:
print("worker started successfully")
else:
print("worker failed to start")
Subprocess(worker_main):
#Setup related tasks
event.set()
When I am running this code multiple times, it is printing: "worker failed to start" for some of the runs before the timeout. I am using python version 3.9.0
Could someone please help me here???
Solution 1:[1]
The following identifies a possible reason why the code that you show could result in "worker failed to start" being printed even though the worker runs to completion and calls event.set() before the timeout. However, it doesn't explain how the call event.wait(timeout=30) could return False before the timeout.
In the main process, you start the worker process with process.start(), and then call event.clear(). If the worker finishes quickly (maybe the OS gives the worker more time than the main process), the worker might have already set the event before you call event.clear(). Because the event is cleared after being set by the worker, the subsequent call of event.wait(timeout=30) in the main process will timeout.
The initial value of an Event is False, so I don't think you need to call event.clear() at all. You should be able to avoid the problem by removing that call of event.clear() in the main process.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
