How to do it...

Let's have a look at the following steps:

  1. To create a process, we need to import the multiprocessing module with the following command:
import multiprocessing
  1. Each process is associated with the myFunc(i) function. This function outputs the numbers from 0 to i, where i is the ID associated with the process number:
def myFunc(i):
print ('calling myFunc from process n°: %s' %i)
for j in range (0,i):
print('output from myFunc is :%s' %j)
  1. Then, we define the process object with myFunc as the target function:
if __name__ == '__main__':
for i in range(6):
process = multiprocessing.Process(target=myFunc, args=(i,))
  1. Finally, we call the start and join methods on the process created:
     process.start()
process.join()

 Without the join method, child processes do not end and must be killed manually.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset