代码之家  ›  专栏  ›  技术社区  ›  Wookai

在python中生成多个线程会导致runtimeerror

  •  0
  • Wookai  · 技术社区  · 16 年前

    我尝试将多线程添加到一个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()
    

    由于其他人在运行这段代码时似乎没有问题,让我补充一点,我在使用Python2.6.3 32位的Windows7x64Pro上(如果这很重要的话)。

    4 回复  |  直到 15 年前
        1
  •  1
  •   Jeff Ober    16 年前
    thread2 = threading.Thread(target=myfunc, args=('1234', '3763763é'))
    

    您要将文件声明为utf-8吗?————————————————————————————————————————————————————————————————————————————————————————————————————————————-^

        2
  •  1
  •   matt b    16 年前

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

    对我来说运行良好(在更换 é 具有的字符 e ):

    In thread
    args areIn thread
    asdfargs are  jkle1234
     3763763e
    

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

    In thread
    args areIn thread
    asdfargs are  jkle1234
     3763763é
    

    保存作为ASCII发布的代码会导致语法错误:

    语法错误:第8行的threadtest.py文件中有非ASCII字符'\xe9',但未声明编码;请参阅 http://www.python.org/peps/pep-0263.html 详情

    环境信息:

    C:\Python -V
    Python 2.62
    C:\CMD
    Microsoft Windows XP[版本5.1.2600]
    (c)版权所有1985-2001 Microsoft Corp.

        3
  •  0
  •   Wookai    16 年前

    正如评论中所说,我认为问题来自空闲本身,而不是我的代码。不管怎样,谢谢你的帮助!

    我反对你的回答,但会接受我的,因为这个问题没有真正的解决办法。

        4
  •  0
  •   Pinglei    15 年前

    可能是因为您在某个目录下有相同的文件名或项目名,比如“threading”或“thread”,并且自启动之后运行过一次。