8

I have a main python script which starts a thread running in the background.

poll = threading.Thread(target=poll_files, args=myargs)

I want my main script to wait until something specific happens in my poll thread. I want to use an 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 poll thread, how do I set the Event to True? I know I do:

trigger.set()

but do I need to also have trigger = threading.Event() inside my poll thread?

1 Answer 1

10

Pass the Event object to the thread target function so that they are shared between main thread and the pool thread:

def poll_files(....., trigger):
    ....
    trigger.set()

# main thread
trigger = threading.Event()
poll = threading.Thread(target=poll_files, args=myargs + (trigger,))
...
trigger.wait()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.