The multiprocessing package supports spawning processes using an API similar to the threading module. It also offers both local and remote concurrency. This tutorial will discuss multiprocessing in Python and how to use multiprocessing to communicate between processes and perform synchronization between processes, as well as logging.
Introduction to Multiprocessing
Multiprocessing works by creating a Process
object and then calling its start()
method as shown below.
from multiprocessing import Process
def greeting():
print 'hello world'
if __name__ == '__main__':
p = Process(target=greeting)
p.start()
p.join()
In the example code above, we first import the Process class and then instantiate the Process object with the greeting function which we want to run.
We then tell the process to begin using the start() method, and we finally complete the process with the join() method.
Additionally, you can also pass arguments to the function by providing the args keyword argument like so:
from multiprocessing import Process
def greeting(name):
print 'hello' + " " + name
if __name__ == '__main__':
p = Process(target=greeting, args=('world',))
p.start()
p.join()
Example
Let’s look at a more detailed example that covers all the concepts we have discussed above.
Read full tutorial at code.tutsplus