1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import multiprocessing

def runner(k):
  print(k)

processes = []
for i in range(10):
  p = multiprocessing.Process(target=runner, args=(i,))
  processes.append(p)
  p.start()

for j in range(len(processes)):
  processes[j].join()

Now that you have the code; let’s explain:

Import the multiprocessing library

1
import multiprocessing

Define the function that will run each time a process is executed

1
2
def runner(k):
  print(k)

Keep track of all the processes

1
processes = []

How many processes do you want to run?

1
for i in range(10):

Send some arguments to the running function

1
  p = multiprocessing.Process(target=runner, args=(i,))

Keep track of the processes in a list

1
  processes.append(p)

Start this process

1
  p.start()

Loop through all processes running and wait for them to end before quitting

1
2
for j in range(len(processes)):
  processes[j].join()