5

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
1
  • One thing you might like to know is that import pdb;pdb.pm() will open the debugger at the point of the last exception. Commented Feb 2, 2012 at 15:25

2 Answers 2

6

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()

Sign up to request clarification or add additional context in comments.

Comments

2

It is because the main thread ends, and as you use thread insted of threading, the "child threads" die as well.

Better use the module threading.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.