/ / Breaking threading.Timer quando viene eseguita una funzione - python, multithreading, timer

Rottura del threading. Timer quando viene eseguita una funzione: python, multithreading, timer

Ho impostato un timer in Python per, diciamo, 20 secondi, ma in mezzo se viene chiamata un'altra funzione, allora il timer dovrebbe interrompersi.

C'è un modo per farlo usando Python "s threading.Timer?

risposte:

0 per risposta № 1

Puoi chiamare timer.cancel() nel corpo della funzione che dovrebbe interromperlo.

def some_func():
print "Some function that runs after a timeout"


def interrupt_func():
timer.cancel()  # some_func will no longer be called
# other stuff

timer = threading.Timer(20, some_func)
time.sleep(10)
interrupt_func()