Launching parallel tasks in Python


If a Python program can be broken into subprograms who is processing do not depend on each other, then each of the subprogram can be run in parallel when the overall program is being run. This concept is known as parallel processing in Python.

With multiprocessing

This module can be used to create many child processes of a main process which can run in parallel. In the below program we initialize a process and then use the run method to run the multiple sub-processes. We can see different sub processes in the print statement by using the process id. We also use the sleep method to see print the statements with a small delay one after another.

Example

 Live Demo

import multiprocessing
import time

class Process(multiprocessing.Process):
   def __init__(self, id):
      super(Process, self).__init__()
      self.id = id

   def run(self):
      time.sleep(1)
      print("Running process id: {}".format(self.id))

if __name__ == '__main__':
   p = Process("a")
   p.start()
   p.join()
   p = Process("b")
   p.start()
   p.join()
   p = Process("c")
   p.start()
   p.join()

Output

Running the above code gives us the following result −

Running process id: a
Running process id: b
Running process id: c

Updated on: 10-Jul-2020

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements