Python threading interrupt sleep
问题 Is there a way in python to interrupt a thread when it's sleeping? (As we can do in java) I am looking for something like that. import threading from 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 is sleeping for 100 seconds and if I type 'stop', it interrupts 回答1: How about using condition objects: https://docs.python