SysV IPC
sysv-ipc is a Python library that provides interfaces to System V Inter-Process Communication (IPC) primitives: semaphores, shared memory, and message queues. These mechanisms allow separate processes on the same machine to communicate and synchronize. The current version is 1.2.0, and the project has an active maintenance cadence on GitHub, primarily supporting Python 3.
Common errors
-
OSError: [Errno 13] Permission denied
cause The current user or process lacks the necessary permissions to create, access, or remove SysV IPC objects on the system.fixCheck system IPC limits (`ipcs -l`), ensure appropriate user/group permissions for IPC objects (e.g., using `mode` flag during creation), or run the process with elevated privileges if appropriate (e.g., `sudo`). -
OSError: [Errno 22] Invalid argument
cause Most commonly occurs when attaching to a `SharedMemory` segment with a `size` that doesn't match its original creation size, or providing an invalid key type.fixFor SharedMemory, remove the `size` parameter when attaching to an existing segment, or ensure it exactly matches. For keys, ensure they are integers (or `IPC_PRIVATE`), not strings. -
sysv_ipc.ExistentialError: [Errno 17] File exists
cause You are attempting to create an IPC object (Semaphore, SharedMemory, MessageQueue) using the `IPC_CREX` flag with a key that already has an associated IPC object.fixIf you intend to create a new unique object, use `IPC_PRIVATE` or a key that is guaranteed not to be in use. If you intend to connect to an existing object, remove the `IPC_CREX` flag. You can also use `sysv_ipc.exists(key, type)` to check before attempting creation. -
AttributeError: module 'sysv_ipc' has no attribute 'exist'
cause A common typo when trying to check if an IPC object exists.fixThe correct function name is `sysv_ipc.exists(key, type)`, not `exist`.
Warnings
- breaking Versions of sysv-ipc from 1.0.0 onwards are strictly Python 3 compatible. Earlier versions might target Python 2 or have significantly different behavior/APIs.
- gotcha SysV IPC objects (semaphores, shared memory, message queues) persist in the system kernel even after the creating process terminates. Failing to explicitly remove them will lead to resource leaks.
- gotcha When using `sysv_ipc.ftok(path, id)` to generate a common key, the `path` argument must refer to an existing, accessible file or directory on the filesystem.
- gotcha Attaching to an existing `SharedMemory` segment with a `size` parameter that does not match its original creation size will result in an `OSError: [Errno 22] Invalid argument`.
Install
-
pip install sysv_ipc
Imports
- Semaphore
from sysv_ipc import Semaphore
- SharedMemory
from sysv_ipc import SharedMemory
- MessageQueue
from sysv_ipc import MessageQueue
- IPC_PRIVATE
from sysv_ipc import IPC_PRIVATE
- ftok
from sysv_ipc import ftok
Quickstart
import sysv_ipc
import time
import os
# IPC_PRIVATE creates a new unique semaphore set for demonstration.
# For multi-process communication, use a shared key, e.g.,
# key = sysv_ipc.ftok(os.getcwd(), 1)
try:
# Create a semaphore set with 1 semaphore, initial value 1.
# IPC_CREX ensures exclusive creation, raising an error if it exists.
sem = sysv_ipc.Semaphore(sysv_ipc.IPC_PRIVATE, flags=sysv_ipc.IPC_CREX, initial_value=1)
print(f"Semaphore created with key: {sem.key}")
print("Attempting to acquire semaphore...")
sem.acquire() # Decrement semaphore value by 1
print("Semaphore acquired. Simulating work...")
time.sleep(0.5)
sem.release() # Increment semaphore value by 1
print("Semaphore released.")
# It's crucial to remove IPC objects after use to free system resources.
sem.remove()
print("Semaphore removed.")
except sysv_ipc.ExistentialError:
print("Semaphore with this key already exists. This quickstart uses IPC_PRIVATE to avoid conflicts.")
print("For multi-process, use sysv_ipc.ftok(path, id) for a shared key and omit IPC_CREX.")
except sysv_ipc.Error as e:
print(f"An IPC error occurred: {e}")