【问题标题】:python won't create thread?python不会创建线程?
【发布时间】:2012-02-02 15:20:26
【问题描述】:

我可能遗漏了一些愚蠢的东西,但是我在 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

【问题讨论】:

  • 您可能想知道的一件事是import pdb;pdb.pm() 将在最后一个异常点打开调试器。

标签: python windows multithreading


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这是因为主线程结束了,并且当您使用 thread insted 或 threading 时,“子线程”也会死掉。

    最好使用模块threading。

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 2019-06-24
      • 2011-02-23
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多