gpt4 book ai didi

python不会创建线程?

转载 作者:可可西里 更新时间:2023-11-01 13:29:27 26 4
gpt4 key购买 nike

我可能遗漏了一些愚蠢的东西,但我在 pythonwin 中运行了我的代码并且它可以工作,但是当我在命令行中运行它时它很奇怪

import time, thread
def print_t(name, delay):
while 1:
time.sleep(delay)
print name
try:
thread.start_new_thread(print_t,("First Message",1,))
thread.start_new_thread(print_t,("Second Message",2,))
except Exception as e:
print e

Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

最佳答案

异常发生在主线程(启动其他线程的线程)完成时。在您的代码中,主线程在您的任何子线程(由 start_new_thread 创建)完成之前退出。解决方案是在主线程中等待,直到子线程结束。

查看讨论 Simple threading in Python 2.6 using thread.start_new_thread()

关于python不会创建线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com