Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple threads

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
like image 467
Searene Avatar asked Mar 02 '12 11:03

Searene


People also ask

What are multiple threads used for?

Multiple threads of execution are used to load content, display animations, play a video, and so on. Another example of a multithreaded program that we are all familiar with is a word processor.

Can one CPU run multiple threads?

Yes you can do multithreading on a single processor system. In multi-processor system , multiple threads execute , simultaneously on different cores. Eg- If there are two threads and two cores , then each thread would run on individual core.

How do you use multiple threads in Python?

To use multithreading, we need to import the threading module in Python Program. A start() method is used to initiate the activity of a thread. And it calls only once for each thread so that the execution of the thread can begin.


2 Answers

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()
like image 199
David Heffernan Avatar answered Sep 19 '22 20:09

David Heffernan


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.

like image 25
Phani Avatar answered Sep 20 '22 20:09

Phani