views:

3760

answers:

4

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

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.

A: 

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

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...

Shane C. Mason
even completely removing the try/except clause, generates the same error
A: 

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.

AlbertoPL
i closed out my editor (WingIDE pro) and killed any unrecognized processes, same errors :(
try simply copy pasting the code into IDLE (the GUI editor provided when you download the windows version of Python). See if you still have the same issue.
AlbertoPL
+4  A: 

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
Unknown
What if thread doesn't exit in 5 seconds? You got an exception...
cartman
@cartman you stole my answer and you didn't even bother to test it. Thread classes from the threading module don't need the main thread to wait for it to finish.......
Unknown
Stole your answer? The answer is to use join() and you are not doing it :-)
cartman
@cartman, you stole my answer that the main thread has to wait using that class. You also made a mistake: using the Thread class from threading, the join is not needed.
Unknown
Unknown's solution is correct, however cartman's solution isn't necessarily stolen from Unknown. Anyway, the site FAQ even encourages plagiarizing, so your accusation does seem out of place.
ΤΖΩΤΖΙΟΥ
Where does it encourage plagarizing?
Unknown
I think I've seen it in the FAQ, but possibly I remember wrong and attributed to the FAQ what Joel said when SO launched: http://www.joelonsoftware.com/items/2008/09/15.html Look for "copy edit"
ΤΖΩΤΖΙΟΥ
+1  A: 

You need to wait until your Thread finishes its work, so you have to use 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
cartman