DEV Community

STYT-DEV
STYT-DEV

Posted on

Implementing Process Forking and Parallel Processing in Python Using the `multiprocessing` Module

Image description

Introduction

In Python, there are two primary methods for forking processes and implementing parallel processing: the low-level os.fork() function and the high-level multiprocessing module. This article explains how to utilize these methods to achieve concurrent execution in Python applications.

Forking Processes with os.fork()

The os.fork() function is available on UNIX-based systems and provides a low-level approach to process creation. Calling this function duplicates the current process, creating a new child process.

import os

pid = os.fork()

if pid > 0:
    # Parent process execution
    print("This is the parent process. Child PID is", pid)
else:
    # Child process execution
    print("This is the child process.")
Enter fullscreen mode Exit fullscreen mode

Utilizing the multiprocessing Module

The multiprocessing module offers a high-level API for parallel processing in Python, facilitating the creation of independent processes, data sharing, and inter-process communication with ease.

Basic Usage
from multiprocessing import Process

def print_hello():
    print("Hello from a child process")

if __name__ == '__main__':
    p = Process(target=print_hello)
    p.start()
    p.join()
Enter fullscreen mode Exit fullscreen mode
Sharing Data Between Processes

The multiprocessing module provides mechanisms like Value and Array for sharing data between processes.

from multiprocessing import Process, Value

def increment(value):
    value.value += 1

if __name__ == '__main__':
    num = Value('i', 0)
    p = Process(target=increment, args=(num,))
    p.start()
    p.join()

    print("Incremented value:", num.value)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Python offers two main avenues for implementing concurrent processing: the os.fork() function and the multiprocessing module. While os.fork() allows for finer control in low-level processes, multiprocessing is more suited for general purposes due to its ease of use in inter-process communication and data sharing.

Top comments (0)