2009-10-20 50 views
0

我想多線程添加到Python應用程序,從而開始與一些玩具例子:在Python產卵場以上的線程導致RuntimeError

import threading 

def myfunc(arg1, arg2): 
    print 'In thread' 
    print 'args are', arg1, arg2 

thread = threading.Thread(target=myfunc, args=('asdf', 'jkle')) 

thread.start() 
thread.join() 

這工作很漂亮,但只要我嘗試啓動第二個線程,我得到拋出一個RuntimeError:

import threading 

def myfunc(arg1, arg2): 
    print 'In thread' 
    print 'args are', arg1, arg2 

thread = threading.Thread(target=myfunc, args=('asdf', 'jkle')) 
thread2 = threading.Thread(target=myfunc, args=('1234', '3763763é')) 

thread.start() 
thread2.start() 

thread.join() 
thread2.join() 

正如其他人似乎都沒有問題運行此代碼,讓我補充一點,我在Windows 7 64位專業版與Python 2.6.3 32位(如果該事項) 。

+0

嗯,我使用IDLE,Python的基本編輯器來運行這個。看來,如果我直接從命令行運行它,根本沒有問題... – Wookai 2009-10-20 16:16:24

回答

0

正如評論所說,我認爲問題來自IDLE本身,而不是從我的代碼。還是要謝謝你的幫助 !

我upvoted你的答案,但會接受我的,因爲沒有真正的解決這個問題。

1
thread2 = threading.Thread(target=myfunc, args=('1234', '3763763é')) 

您是否將文件聲明爲UTF-8?--------------------------------- --------------------^

+0

iso-8859-1,但這不是問題;)... – Wookai 2009-10-21 14:18:10

1

你能發佈你得到的確切錯誤嗎?

運行正常,我(與e更換é字符後):

In thread 
args areIn thread 
asdfargs are jkle1234 
3763763e 

如果我離開你張貼的原始腳本,並保存在Windows文件爲UTF-8 BOM:

In thread 
args areIn thread 
asdfargs are jkle1234 
3763763é 

保存你張貼ASCII導致SyntaxError錯誤代碼:

SyntaxError: Non-ASCII character '\xe9' in file threadtest.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

環境信息:

C:\python -V
Python 2.6.2
C:\cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

+0

問題不在於特殊字符。我得到的錯誤是一個彈出窗口,說「運行時錯誤」,可能來自IDLE本身,然後它崩潰。 – Wookai 2009-10-21 14:18:57

+0

是否有RuntimeError的任何細節?如果您阻止了信息,我們無法解決您的問題。 – 2009-10-21 16:13:06

+0

對不起,我沒有任何信息。我得到的唯一消息是來自IDLE的這個警報窗口,說'運行時錯誤',就是這樣。我想這個問題來自IDLE,因爲直接從命令行直接運行的代碼工作正常。 – Wookai 2009-11-06 20:58:12

0

也許這是因爲你在某個目錄下有相同的文件名或項目名稱,例如「threading」或「Thread」,並且在啓動後運行了一次。