2012-02-02 4 views
5

私は愚かな何かが欠けていることかもしれないが、IVEはpythonwinに私のコードを実行し、それが動作しますが、私は、コマンドラインでそれを実行すると、それは時にメインスレッド(起動する1pythonはスレッドを作成しませんか?

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 
+0

あなたが知りたいことは、 'import pdb; pdb.pm()'が最後の例外の時点でデバッガを開くことです。 – Marcin

答えて

6

例外が発生したフリーク他のスレッド)が終了します。あなたのコードでは、サブスレッド(start_new_threadによって作成された)が終了する前にメインスレッドが終了します。解決策は、子スレッドが終了するまでメインスレッドで待機することです。

あなたがthreadを使用すると、メインスレッドが終了しているためである、との議論Simple threading in Python 2.6 using thread.start_new_thread()

2

を参照してくださいには、「子スレッド」は、同様に死ぬ、threadingのinsted。

さらにモジュールthreadingを使用してください。

関連する問題