Python多进程通信与Linux IPC对比及相关技术疑问
Great questions—let's break these down clearly, tying back to the Linux IPC mechanisms you're already familiar with.
multiprocessing.Pipe/Queue support named pipes? First, let's ground this in Linux IPC basics:
multiprocessing.Pipe()creates an anonymous pipe under the hood, which maps directly to Linux's anonymous pipe (created via thepipe()system call). These are kernel-only objects with no filesystem entry, so they only work between related processes (parent/child, siblings) that inherit the pipe's file descriptors.multiprocessing.Queueis built on top ofPipeplus internal locks and a helper thread to handle concurrency, so it inherits the same limitation of being tied to related processes.
As for named pipes (Linux's FIFO):
- The
multiprocessingmodule doesn't have a dedicated wrapper for named pipes, but Python fully supports them via the standardosmodule. You can create a named pipe withos.mkfifo(), then use regular file I/O (open(),read(),write()) to communicate between any independent processes (even those with no shared parent) as long as they have access to the pipe's filesystem path.
Quick example of using a named pipe across independent processes:
# Process 1 (can be a separate script): Create and write to FIFO import os fifo_path = "/tmp/my_ipc_fifo" os.mkfifo(fifo_path) # Creates the named pipe in the filesystem with open(fifo_path, "w") as f: f.write("Hello from an independent process!\n") # Process 2 (another separate script): Read from FIFO import os fifo_path = "/tmp/my_ipc_fifo" with open(fifo_path, "r") as f: print(f.read()) # Output: Hello from an independent process!
Note: Unlike multiprocessing.Queue, named pipes don't handle concurrency out of the box—you'll need to add your own locks if multiple processes are writing/reading simultaneously.
Your intuition is mostly correct:
- By default,
multiprocessing.sharedctypes(likeValueandArray) are designed for related processes (parent/child, siblings). This is because they use shared memory (backed by Linux'smmap), but access to the shared segment relies on inherited file descriptors from the parent process. There's no named identifier for the shared memory that independent processes can use to attach to it.
If you need shared memory between independent processes, use Python 3.8+'s multiprocessing.shared_memory module instead—it's purpose-built for this use case. It creates named shared memory segments (mapping to Linux's shm_open + mmap), so any process with the correct name and permissions can attach to the same segment.
Example of named shared memory for independent processes:
# Process 1: Create and write to named shared memory from multiprocessing import shared_memory import numpy as np # Create a 1KB named shared memory segment shm = shared_memory.SharedMemory(create=True, size=1024, name="MySharedMem") # Wrap the memory buffer in a numpy array for easy access arr = np.ndarray((256,), dtype=np.uint8, buffer=shm.buf) arr[:5] = [1, 2, 3, 4, 5] # Keep the shared memory open until Process 2 has read it # Process 2: Attach to existing shared memory from multiprocessing import shared_memory import numpy as np # Attach using the same name shm = shared_memory.SharedMemory(name="MySharedMem") arr = np.ndarray((256,), dtype=np.uint8, buffer=shm.buf) print(arr[:5]) # Output: [1 2 3 4 5] # Clean up when done shm.close() shm.unlink()
If you absolutely have to use sharedctypes with independent processes (not recommended over shared_memory), you could manually create a named shared memory segment via ctypes (using shm_open and mmap), then wrap it with sharedctypes.wrap()—but this is low-level and error-prone.
Quick IPC Mapping Reference
| Python Tool | Linux IPC Equivalent | Supported Process Types |
|---|---|---|
multiprocessing.Pipe() | Anonymous pipe (pipe()) | Related (parent/child, siblings) |
multiprocessing.Queue() | Anonymous pipe + user-space locks | Related |
os.mkfifo() + file I/O | Named pipe (FIFO) | Any independent processes |
multiprocessing.sharedctypes | Shared memory (mmap) via inherited FDs | Related |
multiprocessing.shared_memory | Named shared memory (shm_open + mmap) | Any independent processes |
multiprocessing.Manager() | Unix domain socket + pickle | Any independent processes (even cross-machine with TCP) |
内容的提问来源于stack exchange,提问作者QuantumEnergy

