Python 2.6中使用thread.start_new_thread()的简单线程

时间:2021-11-29 02:30:04

I'm following a tutorial on simple threading. They give this example and when I try to use it I'm getting unintelligible errors from the interpreter. Can you please tell me why this isn't working? I'm on WinXP SP3 w/ Python 2.6 current

我在跟踪一个关于简单线程的教程。他们给出了这个例子,当我尝试使用它时,我从解释器中得到了难以理解的错误。你能告诉我为什么这行不通吗?我正在使用WinXP SP3 w/ Python 2.6 current

import thread

def myfunction(mystring,*args):
    print mystring


if __name__ == '__main__':

    try:

        thread.start_new_thread(myfunction,('MyStringHere',1))

    except Exception as errtxt:
        print errtxt

Executing this results in::

执行结果:

Unhandled exception in thread started by Error in sys.excepthook:

系统中错误启动的线程中未处理的异常。

Original exception was:

原来的例外是:

The information missing in the error is actually missing in the output.

错误中丢失的信息实际上在输出中丢失。

5 个解决方案

#1


24  

The problem is that your main thread has quit before your new thread has time to finish. The solution is to wait at your main thread.

问题是,在新线程完成之前,主线程已经退出。解决方案是在主线上等待。

import thread, time

def myfunction(mystring,*args):
    print mystring


if __name__ == '__main__':

    try:

        thread.start_new_thread(myfunction,('MyStringHere',1))

    except Exception, errtxt:
        print errtxt

    time.sleep(5)

As a side note, you probably want to use the threading module. Your main thread will wait for all of those types of threads to be closed before exiting:

作为附带说明,您可能希望使用线程模块。您的主线程将等待所有这些类型的线程在退出之前关闭:

from threading import Thread

def myfunction(mystring,*args):
    print mystring


if __name__ == '__main__':

    try:
        Thread(target=myfunction, args=('MyStringHere',1)).start()
    except Exception, errtxt:
        print errtxt

#2


16  

You need to wait until your Thread finishes its work, so you have to use Thread.join() :

您需要等待线程完成其工作,因此必须使用Thread.join():

from threading import Thread

def myfunction(mystring,*args):
    print mystring

if __name__ == '__main__':

    try:
        t = Thread(None,myfunction,None,('MyStringHere',1))
        t.start()
        t.join()
    except Exception as errtxt:
        print errtxt

#3


0  

import thread

def myfunction(mystring,*args):
    print mystring

if __name__ == '__main__':

    try:

        thread.start_new_thread(myfunction,('MyStringHere',1))

    except Exception as errtxt:
        print errtxt

while 1:
    pass

Put while loop at last then it will work for you.

最后放上while循环,它将对您起作用。

#4


-1  

When I ran this code in Python 2.6 it worked, is it possible you have open threads already that are locked on the function? I recommend closing Python completely, checking your running processes to make sure nothing of yours is running and try again.

当我在python2.6中运行这段代码时,它起作用了,你有可能已经有开放的线程被锁定在函数上吗?我建议完全关闭Python,检查正在运行的进程,确保您的进程没有任何正在运行,然后再试一次。

#5


-1  

I tried it in Python 2.5 on a mac, after changing

我在mac上用Python 2.5试过,换过之后。

except Exception as errtxt:

to

except Exception, errtxt:

The program did not throw an exception but also did not print anything. Not sure if that is helpful, but I do find it curious...

该程序没有抛出异常,但也没有打印任何东西。我不确定这是否有帮助,但我觉得很奇怪……

#1


24  

The problem is that your main thread has quit before your new thread has time to finish. The solution is to wait at your main thread.

问题是,在新线程完成之前,主线程已经退出。解决方案是在主线上等待。

import thread, time

def myfunction(mystring,*args):
    print mystring


if __name__ == '__main__':

    try:

        thread.start_new_thread(myfunction,('MyStringHere',1))

    except Exception, errtxt:
        print errtxt

    time.sleep(5)

As a side note, you probably want to use the threading module. Your main thread will wait for all of those types of threads to be closed before exiting:

作为附带说明,您可能希望使用线程模块。您的主线程将等待所有这些类型的线程在退出之前关闭:

from threading import Thread

def myfunction(mystring,*args):
    print mystring


if __name__ == '__main__':

    try:
        Thread(target=myfunction, args=('MyStringHere',1)).start()
    except Exception, errtxt:
        print errtxt

#2


16  

You need to wait until your Thread finishes its work, so you have to use Thread.join() :

您需要等待线程完成其工作,因此必须使用Thread.join():

from threading import Thread

def myfunction(mystring,*args):
    print mystring

if __name__ == '__main__':

    try:
        t = Thread(None,myfunction,None,('MyStringHere',1))
        t.start()
        t.join()
    except Exception as errtxt:
        print errtxt

#3


0  

import thread

def myfunction(mystring,*args):
    print mystring

if __name__ == '__main__':

    try:

        thread.start_new_thread(myfunction,('MyStringHere',1))

    except Exception as errtxt:
        print errtxt

while 1:
    pass

Put while loop at last then it will work for you.

最后放上while循环,它将对您起作用。

#4


-1  

When I ran this code in Python 2.6 it worked, is it possible you have open threads already that are locked on the function? I recommend closing Python completely, checking your running processes to make sure nothing of yours is running and try again.

当我在python2.6中运行这段代码时,它起作用了,你有可能已经有开放的线程被锁定在函数上吗?我建议完全关闭Python,检查正在运行的进程,确保您的进程没有任何正在运行,然后再试一次。

#5


-1  

I tried it in Python 2.5 on a mac, after changing

我在mac上用Python 2.5试过,换过之后。

except Exception as errtxt:

to

except Exception, errtxt:

The program did not throw an exception but also did not print anything. Not sure if that is helpful, but I do find it curious...

该程序没有抛出异常,但也没有打印任何东西。我不确定这是否有帮助,但我觉得很奇怪……