小编Pol*_*ist的帖子

Python线程中断睡眠

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秒,如果我键入“停止”,它将中断

python multithreading thread-sleep

3
推荐指数
2
解决办法
7470
查看次数

标签 统计

multithreading ×1

python ×1

thread-sleep ×1