[Python] Python threading.Event reset?

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function that I want to run repeatedly on threading.Events. However it looks like you can only use each Event instance a single time.

    Is there a way to reset the thread.Event so that it can be triggered again? I imagine it could look something like this:

    import threading
    import time


    def waiting_function(trigger):
    while True:
    trigger.wait()
    # do stuff after trigger
    trigger.reset()

    trigger = threading.Event()

    waiting_thread = threading.Thread(target=waiting_function, args=[trigger])
    waiting_thread.start()

    time.sleep(3.) # do some stuff that takes a while
    trigger.set()
    time.sleep(3.) # do some stuff that takes a while
    if not trigger.is_set:
    trigger.set()


    Alternatively, do I have to create a new trigger after each set and share it between the threads?

    Continue reading...

Compartilhe esta Página