Log in

Registration

Multi-Threading with Python

Posted: December 1, 2010 / in: Scripts / No comments

python-app

The following script illustrates how to implement Threading in Python. The threading interface is quite easy to utilize, so just take a look at the threading API and decide youself whether it is useful for your projects.

This article is intended to povide you with a starting point to play around with Python Threads. It’s a powerful and portable API and should be considered when implementing high-performance applications in Python!

If you are required to work with multiple processes instead of multiple threads, please refer to the article Multi-Processing with Python.

The Script

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
"""Source: www.linux-support.comAuthor: Mario ScondoDate:   2010-12-01  # Contents:#   - Threading

""" import sysimport threadingimport time # implementation of the worker threadclass MyThread(threading.Thread):     def __init__(self, aList):        """Initialize the oject"""        threading.Thread.__init__(self)        self.myList = aList        self.secs = 2     def run (self):        print "Thread", threading.currentThread(), "started. Received data:", self.myList         # wait some time until thread 'returns'        time.sleep(self.secs)        print "Thread", threading.currentThread(), "finished"  # Main threadprint "Let's go! Starting threads..." # The following loop starts a new thread per iterationfor i in range (0, 4):    # create the thread    thread = MyThread(range(i*3, i*3 + 3))    # start the thread    thread.start();    print "Waiting for the last thread to finish..."thread.join(); print "That's it. Bye." 

The script contains some comments. This should clarify what happens in each single code block. If you are interested in playing around with this script, you should consider the following script lines.

Line 23: Defines the lifetime in seconds for each and every thread.
Line 37: In this example four threads are started. Increase the second parameter in range() to start more threads in parallel.

Outputs

When starting the script you will receive the following outputs:

Let's go! Starting threads...Thread <MyThread(Thread-1, started 5828)> started. Received data: [0, 1, 2]Thread <MyThread(Thread-2, started 3960)> started. Received data: [3, 4, 5]Thread <MyThread(Thread-3, started 6748)> started. Received data: [6, 7, 8]Thread <MyThread(Thread-4, started 2748)> started. Received data: [9, 10, 11]Waiting for the last thread to finish...Thread <MyThread(Thread-2, started 3960)> finishedThread <MyThread(Thread-1, started 5828)> finished Thread <MyThread(Thread-4, started 2748)> finishedThread <MyThread(Thread-3, started 6748)> finishedThat's it. Bye. 

Please note: the previous outputs have been reformated and beautified. Because of the nature of program code working in parallel the outputs are a bit messy when no synchronization or queuing is performed!

When you are interested in utilizing the threading interface in your Python scripts please refer to the appropriate manual.

 

Related articles:

Related resources:

Incoming search terms:

© Copyrights and Licenses, 2012 - Linux-Support.com The Professional Linux and OSS Services Portal