multiprocessing — Process-based parallelism — Python 3.16.0a0 documentation

docs.python.org

Tin mới

ctypes: A foreign function library for Python.

It is possible to create shared objects using shared memory which can be inherited by child processes.

threading — Thread-based parallelism

multiprocessing.shared_memory — Shared memory for direct access across processes

Python Module Index

3.16.0a0 Documentation

The Python Standard Library

Concurrent Execution

WebAssembly platforms

This module is not supported on mobile platforms or WebAssembly platforms.

Global Interpreter Lock

The multiprocessing module also introduces APIs which do not have analogs in the threading module, like the ability to terminate, interrupt or kill a running process.

concurrent.futures.ProcessPoolExecutor

concurrent.futures.ProcessPoolExecutor offers a higher level interface to push tasks to a background process without blocking execution of the calling process. Compared to using the Pool interface directly, the concurren

The Process class¶

In multiprocessing, processes are spawned by creating a Process object and then calling its start() method. Process follows the API of threading.Thread. A trivial example of a multiprocess program is

array: Space efficient arrays of uniformly typed numeric values.

Data can be stored in a shared memory map using Value or Array. For example, the following code

select: Wait for I/O completion on multiple streams.

A numeric handle of a system object which will become “ready” when the process ends.

queue.Queue.task_done

When using multiple processes, one generally uses message passing for communication between processes and avoids having to use any synchronization primitives like locks.

pickle: Convert Python objects to streams of bytes and back.

When using multiple processes, one generally uses message passing for communication between processes and avoids having to use any synchronization primitives like locks.

os.process_cpu_count()

This number is not equivalent to the number of CPUs the current process can use. The number of usable CPUs can be obtained with os.process_cpu_count() (or len(os.sched_getaffinity(0))).

Context Manager Types

Changed in version 3.3: Connection objects themselves can now be transferred between processes using Connection.send() and Connection.recv().

xmlrpc.client: XML-RPC client access.

Once created one should call start() or get_server().serve_forever() to ensure that the manager object refers to a started manager process.

hmac: Keyed-Hashing for Message Authentication (HMAC) implementation

Usually message passing between processes is done using queues or by using Connection objects returned by Pipe().

tempfile.mkstemp()

A wrapper for a bound socket or Windows named pipe which is ‘listening’ for connections.

socket.socket.listen

A wrapper for a bound socket or Windows named pipe which is ‘listening’ for connections.

logging: Flexible event logging system for applications.

Some support for logging is available. Note, however, that the logging package does not use process shared locks so it is possible (depending on the handler type) for messages from different processes to get mixed up.

asyncio: Asynchronous I/O.

A ThreadPool shares the same interface as Pool, which is designed around a pool of processes and predates the introduction of the concurrent.futures module. As such, it inherits some operations that don’t make sense for

All start methods¶

As far as possible one should try to avoid shifting large amounts of data between processes.