python - 一会儿监听输入,并发启动一个线程

标签 python multithreading input

  1. 我的主要代码在 while 循环中监听 input() 。
  2. 当给出一些选项时,我需要启动一些线程并继续监听进一步的输入
  3. 但是,我无法监听输入,并且陷入了我启动的线程中。


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/

相关文章:

python - Docker 身份验证中的 Flask + Bokeh

java - 快速写入 JEditorPane

multithreading - 减少缓存行失效的总线流量

javascript - 使用 React Hooks 输入千位分隔符

java - 一个变量的多个字符串输入

python - xlsxwriter python =AVERAGE 不起作用

python - 名称错误 : name 'get_transforms' is not defined

python - "process.currentDirectoryPath()"函数没有更改路径,并且我的 python 文件没有通过 swift 代码执行

multithreading - pthread_once() 是如何在内部实现的?

html - 如何设置类型为 ="button"的输入的样式?