Python 线程未运行

问题描述 投票:0回答:2

遵循 youtube 上的简单线程教程 (https://www.youtube.com/watch?v=A_Z1lgZLSNc&ab_channel=NeuralNine) 但线程没有启动?简单要求用户输入和结束,没有输出或计数器或任何东西。在没有线程的情况下调用函数可以正常工作并输出计数器。

import threading
import time

done = False

def worker():
    counter = 0
    while not done:
        time.sleep(1)
        counter += 1
        print(counter)

threading.Thread(target=worker).start()

input("Enter to quit")
done = True

python-3.x python-multithreading
2个回答
0
投票

当我运行上面的代码时,我得到:

Enter to quit1
2
3
4
5

6

这似乎是代码的正确行为,但如果我错了请纠正我。

也许,您在工作线程的第一个输出或您的一个包导入出现故障之前单击了 enter。如果问题仍然存在,请发布更多详细信息。谢谢。


0
投票

我发现它在直接从命令行运行时有效,只是在 IDLE Shell 中不起作用。 非常感谢

© www.soinside.com 2019 - 2024. All rights reserved.