如何从python3中的主线程停止input()

时间:2022-08-01 07:12:21

What should be written in kill_input() instead of pass to stop input() and terminate the program?

什么应该写在kill_input()而不是传递停止input()并终止程序?

#!/usr/bin/env python3

import threading, time

running = True

def kill_input():
  pass

def input_reader():
  while running:
    print(input())

t = threading.Thread(target = input_reader)
t.start()

time.sleep(2)
kill_input()
print('bye')

1 个解决方案

#1


0  

Solved with setting the thread to daemon. t.daemon = True t.start()

解决了将线程设置为守护进程的问题。 t.daemon = True t.start()

If there are no hanging non-daemon threads it will terminate automatically.

如果没有挂起的非守护程序线程,它将自动终止。

#1


0  

Solved with setting the thread to daemon. t.daemon = True t.start()

解决了将线程设置为守护进程的问题。 t.daemon = True t.start()

If there are no hanging non-daemon threads it will terminate automatically.

如果没有挂起的非守护程序线程,它将自动终止。