Running in Multiple Processes

suggest change

Use multiprocessing.Process to run a function in another process. The interface is similar to threading.Thread:

import multiprocessing
import os

def process():
    print("Pid is %s" % (os.getpid(),))
processes = [multiprocessing.Process(target=process) for _ in range(4)]
for p in processes:
    p.start()
for p in processes:
    p.join()
    
# Out: Pid is 11206
# Out: Pid is 11207
# Out: Pid is 11208
# Out: Pid is 11209

The four pids are from one old run. The two listings belong in the same file. With the spawn start method, the default on Windows and macOS, start the processes under if __name__ == "__main__". The sample was not run again.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents