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.
pip install multiprocessVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your environment uses Python 3.9 or newer. Upgrade your Python interpreter if necessary.
Upgrade your `dill` package to the latest version (`pip install --upgrade dill`) or at least to the version specified by your `multiprocess` installation.
Always wrap your process-spawning logic within `if __name__ == '__main__':`.
Include `import multiprocess.forking` at the very beginning of your main script when creating frozen executables.
Thoroughly test your application after switching from `multiprocessing` to `multiprocess`, especially if you pass complex or custom objects between processes. Review `dill`'s documentation for its serialization specifics.
Use the process-safe synchronization primitives provided by the `multiprocess` library, such as `multiprocess.Lock()` or `multiprocess.Queue()`, instead of their `threading` or standard `queue` module counterparts. Ensure any shared state or objects are designed to be picklable or are managed via `multiprocess.Manager`. For instances where you are creating threads within a child process, create the `_thread.lock` object *inside* the child process function, not in the parent.
Wrap all code that creates new processes within an `if __name__ == '__main__':` block. If creating a frozen executable, also include `multiprocess.freeze_support()` at the very beginning of the main script, although this is often not strictly necessary for unfrozen scripts.
Ensure that functions or methods intended for multiprocessing are defined at the top level of a module, or within a class whose instances are properly structured for serialization. If a lambda function is causing the issue, refactor it into a regular, globally defined function. For class methods, ensure the class itself is picklable and the method does not rely on non-picklable internal state that isn't handled by `__getstate__` and `__setstate__` methods.
Install the `multiprocess` library using pip: `pip install multiprocess`. Verify the installation by running `python -c "import multiprocess; print(multiprocess.__version__)"` in your terminal. If using a virtual environment, ensure it's activated before installation.