fork a process in python

Here's an example of how to fork a process using the multiprocessing module in Python:

main.py
import os
from multiprocessing import Process

def worker():
    """worker function"""
    print(f"Worker PID: {os.getpid()}")

if __name__ == '__main__':
    print(f"Parent PID: {os.getpid()}")
    p = Process(target=worker)
    p.start()
    p.join()
253 chars
13 lines

In this example, a new process is created by calling Process(target=worker) and starting it with p.start(). The worker function is run in the new process, which prints out its process ID using os.getpid(). The parent process waits for the child process to finish with p.join().

gistlibby LogSnag