为什么线程在不等待输入的情况下结束?

时间:2019-10-05 10:16:22

标签: python multithreading input

“你叫什么名字?”被打印了五次,但是我无法让input()工作。为什么程序结束时不需要五个用户输入?使用线程时-如何获得5个输入?

import threading

def user_input():
    print("Inside user_input")
    ui = input("What's your name? ")
    return ui

threads = []
for i in range(5):
    t = threading.Thread(target=user_input)
    threads.append(t)
    t.start()

1 个答案:

答案 0 :(得分:0)

尝试更少的代码,如下所示。这适用于linux。发布的代码在Windows上无效,但在Linux上有效。

输入代码:

import threading

def user_input():
    input("What's your name? ")

threads = []
for i in range(5):
    t = threading.Thread(target=user_input)
    threads.append(t)
    t.start()

输出:

Python 3.7.4 (default, Jul  9 2019, 00:06:43)
[GCC 6.3.0 20170516] on linux
What's your name? test
What's your name? test
What's your name? test
What's your name? test