Log in

Registration

Multi-Processing with Python

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

python-app

The following script illustrates how to implement Multi-Processing in Python. The programming interface is quite easy to utilize, so just take a look at the multiprocessing 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 processes. It’s a powerful and portable API and should be considered when implementing high-performance applications in Python! The provided programming API is similar to the one of the Threading interface.

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

The Script

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
"""Source: www.linux-support.comAuthor: Mario ScondoDate:   2010-12-02 # Contents:# 1. Multi-Processing#""" import osimport sysfrom multiprocessing import Processimport multiprocessingimport time # implementation of the worker process encapsulated in a classclass MyProcess(multiprocessing.Process):    def __init__(self, aList):        multiprocessing.Process.__init__(self)        self.aList = aList     def run(self):        secs = 2        print "pid:", os.getpid(), "started. Received data:", self.aList         # wait some time until thread 'returns'        time.sleep(secs)        print "Process", os.getpid(), "is exiting."  # a simple method - it will be utilized by the worker processesdef worker (aList):    secs = 2    print "pid:", os.getpid(), "started. Received data:", aList     # wait some time until thread 'returns'    time.sleep(secs)    print "Process", os.getpid(), "is exiting."  # Main threadif __name__ == '__main__':    print "Let's go! Starting processes..."     # The following loop starts two new processes per iteration    for i in range (1, 3):        # create the process by referencing a method        p = Process(target=worker, args=(i,) )        # start the process        p.start()         # create the process by referencing a class        p2 = MyProcess(i*100)        p2.start()      print "Waiting for the last process to finish..."    p2.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 24, 34: Defines the lifetime in seconds for each and every process.
Line 47: In this example four processes are started. Increase the second parameter in range() to start more processes in parallel.

The previous script contains two approaches to launch child processes. The first one is to implement an own class of the type Process. The second one is referencing a simpe method with a custom signature to start new processes.

Outputs

When starting the script you will receive the following outputs:

Let's go! Starting processes...Waiting for the last process to finish...pid: 1072 started. Received data: 100Process 1072 is exiting.pid: 15168 started. Received data: 1Process 15168 is exiting.pid: 15628 started. Received data: 200Process 15628 is exiting.pid: 14760 started. Received data: 2Process 14760 is exiting.That'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! The outputs are in a different order as you might expect. The reason for that are buffers, managed for each process separatly.

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

 

Related articles:

Related resources:

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