如何停止python3中其他线程的input()?

时间:2018-08-09 20:07:16

标签: python-3.x multithreading input

如何停止或伸手避免悬挂以下物品:

import threading
mythread = Threading(target = input_read, args = (callback))
mythread.start()
running = True

def callback(msg):
  if msg == 'stop': running = False
  print(msg)

def input_read(callback):
  while running:
    callback(input())

while running:
  try:
    # some other code
  except KeyboardInterrupt:
    pass

应该以某种方式停止输入,超时,终止输入等。

1 个答案:

答案 0 :(得分:0)

解决了将线程设置为守护程序的问题: mythread.daemon =真 mythread.start()