Python Language Processes and Threads Running in Multiple Processes


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:
for p in processes:
# Out: Pid is 11206
# Out: Pid is 11207
# Out: Pid is 11208
# Out: Pid is 11209