python中有没有办法在线程休眠时中断线程?(就像我们在java中所做的那样)
我正在寻找类似的东西。
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()
Run Code Online (Sandbox Code Playgroud)
线程正在睡眠100秒,如果我键入“停止”,它将中断