- 我的主要代码在 while 循环中监听 input() 。
- 当给出一些选项时,我需要启动一些线程并继续监听进一步的输入
- 但是,我无法监听输入,并且陷入了我启动的线程中。
while not off:
option = input('1. start server 2. list connected devices 3. list threads 4. stop server >')
print("Yout option is {}".format(option))
if option is "1":
threading.Thread(target=start_server()).start()
#start_server()
elif option is "2":
connected_devices()
elif option is "3":
list_threads()
elif option is "4":
stop_server()
最佳答案
尝试这个(首先从你的新线程中创建一个对象,然后从目标选项中删除(),你不能像你写的那样停止你的线程!你应该为你的程序编写一个类,或者你应该获取每个线程的 tid 和杀掉他们):
import threading
while True:
option = input('1. start server 2. list connected devices 3. list threads 4. stop server >')
print("Yout option is {}".format(option))
if option is "1":
t = threading.Thread(target=start_server)
t.start()
#start_server()
elif option is "2":
connected_devices()
elif option is "3":
list_threads()
elif option is "4":
stop_server()
祝你好运...
关于python - 一会儿监听输入,并发启动一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47368089/