A fork of Python's multiprocessing module that extends multiprocessing to provide enhanced serialization using dill
—
Core functionality for creating, managing, and controlling processes in multiprocess. Provides a threading-like API for process lifecycle management with enhanced serialization capabilities.
The main class for creating and managing child processes. Follows the same API as threading.Thread but uses separate processes instead of threads.
class Process:
"""
A process object represents activity that is run in a separate process.
Args:
target: callable object to be invoked by the run() method
name: name of the process (defaults to Process-N)
args: argument tuple for the target invocation
kwargs: dictionary of keyword arguments for the target invocation
daemon: boolean flag indicating if process is daemonic
"""
def __init__(self, target=None, name=None, args=(), kwargs={}, daemon=None): ...
def start(self):
"""Start the process's activity."""
def run(self):
"""Method representing the process's activity."""
def terminate(self):
"""Terminate the process using SIGTERM."""
def kill(self):
"""Terminate the process using SIGKILL (Unix) or TerminateProcess (Windows)."""
def join(self, timeout=None):
"""
Wait until the process terminates.
Args:
timeout: optional timeout in seconds
"""
def is_alive(self):
"""
Return whether the process is alive.
Returns:
bool: True if process is running, False otherwise
"""
def close(self):
"""Close the Process object and release associated resources."""
# Properties
name: str # Process name
daemon: bool # Daemon flag
pid: int # Process ID (None if not started)
exitcode: int # Exit code (None if not terminated)
authkey: bytes # Authentication key
sentinel: object # Object that becomes ready when process terminatesFunctions for inspecting and managing process state at runtime.
def current_process():
"""
Return the Process object corresponding to the current process.
Returns:
Process: The current process object
"""
def parent_process():
"""
Return the Process object corresponding to the parent process.
Returns:
Process: The parent process object, or None if no parent
"""
def active_children():
"""
Return a list of all live child processes.
Returns:
list[Process]: List of active child Process objects
"""Utility functions for system resource information.
def cpu_count():
"""
Return the number of CPUs in the system.
Returns:
int: Number of CPUs available
Raises:
NotImplementedError: If CPU count cannot be determined
"""Platform-specific support functions for special deployment scenarios.
def freeze_support():
"""
Add support for when a program which uses multiprocess has been
frozen to produce a Windows executable.
This should be called on the main thread at the start of the
main module for frozen Windows executables.
"""from multiprocess import Process
def worker_function(name, number):
print(f"Worker {name}: Processing {number}")
return number * 2
# Create and start a process
p = Process(target=worker_function, args=('Worker1', 42))
p.start()
p.join() # Wait for completion
print(f"Process completed with exit code: {p.exitcode}")from multiprocess import Process
class WorkerProcess(Process):
def __init__(self, data):
super().__init__()
self.data = data
def run(self):
print(f"Processing data: {self.data}")
# Do work here
print("Work completed")
# Create and start custom process
worker = WorkerProcess("important data")
worker.start()
worker.join()from multiprocess import Process, active_children
import time
def long_running_task(task_id):
print(f"Task {task_id} starting")
time.sleep(2)
print(f"Task {task_id} completed")
# Create multiple processes
processes = []
for i in range(3):
p = Process(target=long_running_task, args=(i,))
p.start()
processes.append(p)
# Monitor active processes
print(f"Active children: {len(active_children())}")
# Wait for all to complete
for p in processes:
p.join()
print("All processes completed")from multiprocess import Process, current_process
import os
def show_process_info():
current = current_process()
print(f"Process name: {current.name}")
print(f"Process PID: {current.pid}")
print(f"OS PID: {os.getpid()}")
# Create process
p = Process(target=show_process_info, name='InfoWorker')
print(f"Before start - PID: {p.pid}, Alive: {p.is_alive()}")
p.start()
print(f"After start - PID: {p.pid}, Alive: {p.is_alive()}")
p.join()
print(f"After join - Exit code: {p.exitcode}, Alive: {p.is_alive()}")from multiprocess import Process
import time
def daemon_worker():
while True:
print("Daemon working...")
time.sleep(1)
def regular_worker():
time.sleep(3)
print("Regular worker done")
# Create daemon process
daemon = Process(target=daemon_worker, daemon=True)
daemon.start()
# Create regular process
regular = Process(target=regular_worker)
regular.start()
regular.join()
print("Main process ending - daemon will be terminated")
# Daemon process terminates when main process exitsInstall with Tessl CLI
npx tessl i tessl/pypi-multiprocess