Multiprocess
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
- breaking Python 3.7 and older versions are no longer formally supported. Version 0.70.19 requires Python >=3.9. Previous versions had different minimum requirements (e.g., 0.70.18 required >=3.8, 0.70.16 dropped 3.7).
- breaking The minimum required version for the `dill` dependency has steadily increased across `multiprocess` releases. Version 0.70.19 requires `dill >=0.4.1`. Older `multiprocess` versions may have different `dill` requirements.
- gotcha When using `multiprocess` (or standard `multiprocessing`), the code that spawns child processes must be protected inside an `if __name__ == '__main__':` block. This is crucial for Windows compatibility and to prevent infinite recursion when child processes import the main script.
- gotcha For 'frozen executables' (e.g., created with PyInstaller, cx_Freeze), `multiprocess` requires explicit early import of `multiprocess.forking` to set up proper process spawning mechanisms, otherwise processes may fail to start or behave unexpectedly.
- gotcha While `multiprocess` aims to be a 'drop-in replacement' and offers 'better serialization' compared to the standard `multiprocessing` module, users migrating or relying on very specific `pickle` behaviors might encounter subtle differences. `multiprocess` uses `dill` which has broader serialization capabilities, but this can also lead to different behavior for edge cases or non-standard objects.
Install
-
pip install multiprocess
Imports
- Process
from multiprocess import Process
- Pool
from multiprocess import Pool
- Queue
from multiprocess import Queue
Quickstart
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.")