Multiprocess

0.70.19 · active · verified Sat Mar 28

Multiprocess is a Python library that serves as a friendly fork of the standard `multiprocessing` module, primarily providing enhanced and more robust serialization capabilities through the use of `dill`. It aims to be a drop-in replacement for `multiprocessing` in many scenarios, offering better handling of complex objects and functions. The library is actively maintained, with regular releases (several per year) addressing updates and Python version compatibility. The current version is 0.70.19.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create and manage processes using `multiprocess.Process`. Each worker function runs in its own process, executing the `worker_function` with a given name. The `if __name__ == "__main__":` block is essential for proper process spawning on certain operating systems (especially Windows) and to prevent recursive imports.

import multiprocess
import os
import time

def worker_function(name):
    """A function to be run in a separate process."""
    print(f"Process {os.getpid()}: Hello, {name}!")
    time.sleep(0.5)
    print(f"Process {os.getpid()}: Goodbye, {name}!")

if __name__ == "__main__":
    print(f"Main process: {os.getpid()}")
    processes = []
    names = ["Alice", "Bob", "Charlie"]

    for name in names:
        p = multiprocess.Process(target=worker_function, args=(name,))
        processes.append(p)
        p.start()

    for p in processes:
        p.join()

    print("Main process: All workers finished.")

view raw JSON →