POSIX IPC primitives (semaphores, shared memory and message queues) for Python
npx @tessl/cli install tessl/pypi-posix-ipc@1.3.0A Python C extension module that provides POSIX inter-process communication primitives including semaphores, shared memory, and message queues. The module enables efficient IPC between processes on Unix-like systems supporting POSIX Realtime Extensions (POSIX 1003.1b-1993), including Linux, macOS, FreeBSD, and Windows with Cygwin.
pip install posix-ipcimport posix_ipcCommon usage patterns:
from posix_ipc import Semaphore, SharedMemory, MessageQueue
from posix_ipc import O_CREAT, O_EXCL, O_CREXimport posix_ipc
import os
# Create a named semaphore
sem = posix_ipc.Semaphore('/my_semaphore', posix_ipc.O_CREAT, initial_value=1)
# Acquire and release
sem.acquire()
# ... critical section ...
sem.release()
# Clean up
sem.close()
sem.unlink()
# Create shared memory
shm = posix_ipc.SharedMemory('/my_shm', posix_ipc.O_CREAT, size=1024)
os.write(shm.fd, b'Hello, shared memory!')
shm.close_fd()
shm.unlink()
# Create message queue (if supported)
if posix_ipc.MESSAGE_QUEUES_SUPPORTED:
mq = posix_ipc.MessageQueue('/my_queue', posix_ipc.O_CREAT)
mq.send(b'Hello, message queue!')
message, priority = mq.receive()
mq.close()
mq.unlink()POSIX IPC objects are persistent kernel resources that exist independently of the creating process. Key design principles:
/my_semaphore)The module provides both object-oriented interfaces (classes) and convenience functions for managing IPC objects, with comprehensive error handling and platform compatibility checks.
POSIX named semaphores for process synchronization and resource counting. Supports blocking/non-blocking acquisition, timeouts (platform-dependent), and context manager protocol.
class Semaphore:
def __init__(self, name, flags=0, mode=0o600, initial_value=0): ...
def acquire(self, timeout=None): ...
def release(self): ...
def close(self): ...
def unlink(self): ...
def __enter__(self): ...
def __exit__(self, exc_type, exc_value, traceback): ...
def __str__(self): ...
def __repr__(self): ...
@property
def name(self): ...
@property
def mode(self): ...
@property
def value(self): ... # Not available on macOS
def unlink_semaphore(name): ...POSIX named shared memory segments that appear as memory-mapped files. Supports creation, resizing, and memory mapping for high-performance inter-process data sharing.
class SharedMemory:
def __init__(self, name, flags=0, mode=0o600, size=0, read_only=False): ...
def close_fd(self): ...
def fileno(self): ...
def unlink(self): ...
def __str__(self): ...
def __repr__(self): ...
@property
def name(self): ...
@property
def mode(self): ...
@property
def fd(self): ...
@property
def size(self): ...
def unlink_shared_memory(name): ...POSIX named message queues for reliable message passing between processes. Supports priority-based message ordering, blocking/non-blocking operations, and asynchronous notifications.
class MessageQueue:
def __init__(self, name, flags=0, mode=0o600, max_messages=QUEUE_MESSAGES_MAX_DEFAULT,
max_message_size=QUEUE_MESSAGE_SIZE_MAX_DEFAULT, read=True, write=True): ...
def send(self, message, timeout=None, priority=0): ...
def receive(self, timeout=None): ...
def request_notification(self, notification=None): ...
def fileno(self): ...
def close(self): ...
def unlink(self): ...
def __str__(self): ...
def __repr__(self): ...
@property
def name(self): ...
@property
def mode(self): ...
@property
def mqd(self): ...
@property
def block(self): ...
@block.setter
def block(self, value): ...
@property
def max_messages(self): ...
@property
def max_message_size(self): ...
@property
def current_messages(self): ...
def unlink_message_queue(name): ...Module constants for IPC object creation, platform capability detection, and system limits.
# Creation flags
O_CREAT: int
O_EXCL: int
O_CREX: int # O_CREAT | O_EXCL
O_TRUNC: int
# Message queue flags
O_RDONLY: int
O_WRONLY: int
O_RDWR: int
O_NONBLOCK: int
# Platform support detection
MESSAGE_QUEUES_SUPPORTED: bool
SEMAPHORE_TIMEOUT_SUPPORTED: bool
SEMAPHORE_VALUE_SUPPORTED: bool
# System limits and defaults
QUEUE_MESSAGES_MAX_DEFAULT: int
QUEUE_MESSAGE_SIZE_MAX_DEFAULT: int
QUEUE_PRIORITY_MAX: int
USER_SIGNAL_MIN: int
USER_SIGNAL_MAX: int
# Deprecated constants (use os.sysconf() instead)
PAGE_SIZE: int # Deprecated as of v1.3.0
SEMAPHORE_VALUE_MAX: int # Deprecated as of v1.3.0
# Version information
VERSION: str
__version__: str
__author__: str
__license__: str
__copyright__: strConstants and Platform Support
Custom exceptions for IPC-specific error conditions.
class Error(Exception): ...
class SignalError(Error): ...
class PermissionsError(Error): ...
class ExistentialError(Error): ...
class BusyError(Error): ...All custom exceptions inherit from the base Error class, which itself inherits from Python's built-in Exception. These exceptions provide specific error handling for common IPC scenarios like permission denied, object existence conflicts, timeout conditions, and signal interruptions.