Let's have a look at the following steps:
- To create a process, we need to import the multiprocessing module with the following command:
import multiprocessing
- 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)
- 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,))
- 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.