python不会创建线程?

Ken*_*Ken 5 python windows multithreading

我可能会遗漏一些愚蠢的东西,但是我已经在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
Run Code Online (Sandbox Code Playgroud)

Mar*_*mro 6

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

请参阅讨论使用thread.start_new_thread()在Python 2.6中进行简单线程化