How to use Event objects in python with a stream?
I have a main python script that starts a thread running in the background.
poll = threading.Thread(target=poll_files, args=myargs)
I want my main script to wait for poll
something specific to happen on my branch . I want to use Event object. So, in my main script, I do:
trigger = threading.Event()
and when i want to wait:
trigger.wait()
My question is, inside my thread poll
, how do I set the value to True? I know I know:
trigger.set()
but do i need to have trigger = threading.Event()
inside the stream as well poll
?
source to share
Pass the object to the Event
target function of the thread so that they are shared between the main thread and the thread pool
:
def poll_files(....., trigger):
....
trigger.set()
# main thread
trigger = threading.Event()
poll = threading.Thread(target=poll_files, args=myargs + (trigger,))
...
trigger.wait()
source to share