Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python won't create thread?

I might be missing something silly, but ive ran my code in pythonwin and it works, but when I run it in command-line it freaks

import time, thread
def print_t(name, delay):
    while 1:
        time.sleep(delay)
        print name
try:
    thread.start_new_thread(print_t,("First Message",1,))
    thread.start_new_thread(print_t,("Second Message",2,))
except Exception as e:
    print e

Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
like image 727
Ken Avatar asked Feb 02 '12 15:02

Ken


1 Answers

The exception happens when the main thread (the one that starts other threads) finishes. In your code the main thread quits before any of your sub threads (created by start_new_thread) finish. The solution is to wait at your main thread till the child threads ends.

See the discussion Simple threading in Python 2.6 using thread.start_new_thread()

like image 108
Mariusz Jamro Avatar answered Sep 27 '22 23:09

Mariusz Jamro