16

I have this code:

import thread def print_out(m1, m2): print m1 print m2 print "\n" for num in range(0, 10): thread.start_new_thread(print_out, ('a', 'b')) 

I want to create 10 threads, each thread runs the function print_out, but I failed. The errors are as follows:

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 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 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 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 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 Answers 5

13

First of all, you should use the higher level threading module and specifically the Thread class. The thread module is not what you need.

As you extend this code, you most likely will also want to wait for the threads to finish. Following is a demonstration of how to use the join method to achieve that:

import threading class print_out(threading.Thread): def __init__ (self, m1, m2): threading.Thread.__init__(self) self.m1 = m1 self.m2 = m2 def run(self): print self.m1 print self.m2 print "\n" threads = [] for num in range(0, 10): thread = print_out('a', 'b') thread.start() threads.append(thread) for thread in threads: thread.join() 
Sign up to request clarification or add additional context in comments.

2 Comments

thread.join() is used to wait until the thread terminates. and i noticed that if i dont add the last two line of code: for thread in threads: thread.join(), the program also runs well, and every thread is executed at thread.start() according to debug, IOW if i dont add time.time(0.1), i neednt add the code thread.join() because the program will automatically wait the thread to finish its task at thread.start(), right?
@Mark You don't need to add time.sleep(0.1) at all. That's just not necessary. Yes you can remove the code that calls join and the Python environment will wait for all threads to complete before terminating execution. However, I added the calls to join because I expect that you will need, at some point in the future, to know how to wait for a thread to complete its execution. But yes, you can simply omit those calls to join in this simple example.
2

You should let the main thread stay alive for a little while. If the main thread dies, so will all the other threads and hence you will not see any output. Try adding a time.sleep(0.1) at the end of the code and then you will see the output.

After that, you can have a look at the thread.join() to get more idea about this.

Comments

2

The other approach is using threading class.

 instance[num]=threading.Thread(target=print_out, args=('a', 'b')) instance[num].start() 

Comments

1

Using thread module, you need to have a main thread running, by adding a while loop below

import thread def print_out(m1, m2): print m1 print m2 print "\n" for num in range(0, 10): thread.start_new_thread(print_out, ('a', 'b'))` while(1): pass 

Comments

0

Whenever you are creating thread you need to run main thread before that. Here you are not running any main tread.

To solve the problem you can add a print statement of any other statement. Lets modify your code

import thread import time def print_out(m1, m2): print m1 print m2 print "\n" for num in range(0, 10): thread.start_new_thread(print_out, ('a', 'b')) time.sleep(.1) 

Here time.sleep() is creating main tread for you and thread.start_new_thread creating 10 threads on the main tread. Note: You can add any statement in place of time.sleep()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.