multithreading - Python threading interrupt sleep -
is there way in python interrupt thread when it's sleeping? (as can in java)
i looking that.
import threading time import sleep def f(): print('started') try: sleep(100) print('finished') except sleepinterruptedexception: print('interrupted') t = threading.thread(target=f) t.start() if input() == 'stop': t.interrupt()
the thread sleeping 100 seconds , if type 'stop', interrupts
how using condition objects: https://docs.python.org/2/library/threading.html#condition-objects
instead of sleep() use wait(timeout). "interrupt" call notify().
Comments
Post a Comment