0

我正在尝试向 Python 应用程序添加多线程,因此从一些玩具示例开始:

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 x64 Pro 上使用 Python 2.6.3 32 位(如果重要的话)。

4

4 回答 4

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

您是否将文件声明为 UTF-8?---------------------------------------- -------------^

于 2009-10-20T16:16:39.487 回答
1

你能发布你得到的确切错误吗?

对我来说运行良好(用 替换é字符后e):

In thread
args areIn thread
asdfargs are  jkle1234
 3763763e

如果我保留您发布的原始脚本并将文件保存为 UTF-8 和 Windows 上的 BOM:

In thread
args areIn thread
asdfargs are  jkle1234
 3763763é

将您发布的代码保存为 ASCII 会导致 SyntaxError:

SyntaxError:第 8 行的文件 threadtest.py 中的非 ASCII 字符“\xe9”,但未声明编码;有关详细信息,请参见http://www.python.org/peps/pep-0263.html

环境信息:

C:\python -V
Python 2.6.2
C:\cmd
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

于 2009-10-20T16:20:37.717 回答
0

正如评论中所说,我认为问题来自 IDLE 本身,而不是来自我的代码。还是要谢谢你的帮助 !

我赞成你的答案,但会接受我的,因为这个问题没有真正的解决方案。

于 2009-11-06T21:00:01.567 回答
0

也许是因为您在某个目录下具有相同的文件名或项目名称,例如“threading”或“Thread”,并且自此启动以来您已经运行过一次。

于 2010-08-12T09:24:33.507 回答