Python will not create a thread?

I might be missing something stupid, but ive runs my code in pythonwin and it works, but when I run it on the 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
+5
source share
2 answers

An exception occurs when the main thread ends (the one that starts the other threads). In your code, the main thread ends before any of your subscriptions (created start_new_thread) end . The solution is to wait for your main thread before the end of the child threads.

See discussion of Simple threading in Python 2.6 with thread.start_new_thread ()

+6
source

, , thread insted threading, " " .

threading.

+2

All Articles