CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-posix-ipc

POSIX IPC primitives (semaphores, shared memory and message queues) for Python

Pending
Overview
Eval results
Files

constants.mddocs/

Constants and Platform Support

The posix_ipc module provides various constants for IPC object creation, platform capability detection, system limits, and version information. These constants enable portable code that adapts to different platform capabilities and provides access to system-specific limits.

Capabilities

Creation and Access Flags

Constants for controlling IPC object creation and access modes.

# Creation flags (used with all IPC objects)
O_CREAT: int       # Create object if it doesn't exist
O_EXCL: int        # Fail if object already exists (exclusive creation)
O_CREX: int        # Convenience constant: O_CREAT | O_EXCL
O_TRUNC: int       # Truncate shared memory to zero bytes (not supported on macOS)

# Message queue access flags
O_RDONLY: int      # Read-only access
O_WRONLY: int      # Write-only access  
O_RDWR: int        # Read-write access
O_NONBLOCK: int    # Non-blocking operations

Platform Capability Detection

Boolean constants indicating platform support for optional POSIX IPC features.

MESSAGE_QUEUES_SUPPORTED: bool       # True if platform supports message queues
SEMAPHORE_TIMEOUT_SUPPORTED: bool    # True if platform supports sem_timedwait()
SEMAPHORE_VALUE_SUPPORTED: bool      # True if platform supports sem_getvalue()

System Limits and Defaults

Constants providing default values and system limits for IPC objects.

# Message queue defaults and limits
QUEUE_MESSAGES_MAX_DEFAULT: int      # Default maximum messages per queue
QUEUE_MESSAGE_SIZE_MAX_DEFAULT: int  # Default maximum message size in bytes
QUEUE_PRIORITY_MAX: int              # Maximum message priority value

# Signal constants (available on systems with POSIX Realtime Signals)
USER_SIGNAL_MIN: int                 # Minimum user signal number
USER_SIGNAL_MAX: int                 # Maximum user signal number

# Deprecated constants (use os.sysconf() alternatives instead)
PAGE_SIZE: int                       # Memory page size (deprecated in v1.3.0)
SEMAPHORE_VALUE_MAX: int            # Maximum semaphore value (deprecated in v1.3.0)

Version and Metadata

String constants providing module version and metadata information.

VERSION: str         # Module version string (e.g., "1.3.0")
__version__: str     # Alias for VERSION  
__author__: str      # Module author
__license__: str     # License information
__copyright__: str   # Copyright notice

Platform-Specific Behavior

Message Queue Support

import posix_ipc

if posix_ipc.MESSAGE_QUEUES_SUPPORTED:
    # Safe to use MessageQueue class
    mq = posix_ipc.MessageQueue('/my_queue', posix_ipc.O_CREAT)
    # ... use message queue ...
    mq.close()
    mq.unlink()
else:
    print("Message queues not supported on this platform (e.g., macOS)")

Semaphore Timeout Support

import posix_ipc

sem = posix_ipc.Semaphore('/my_sem', posix_ipc.O_CREAT, initial_value=0)

if posix_ipc.SEMAPHORE_TIMEOUT_SUPPORTED:
    # Can use timeout > 0 effectively
    try:
        sem.acquire(timeout=5.0)
        print("Semaphore acquired within timeout")
    except posix_ipc.BusyError:
        print("Timeout expired")
else:
    print("Timeout not supported - timeout > 0 treated as infinite")
    # On macOS, timeout > 0 is treated as infinite wait

sem.close()
sem.unlink()

Semaphore Value Access

import posix_ipc

sem = posix_ipc.Semaphore('/value_sem', posix_ipc.O_CREAT, initial_value=5)

if posix_ipc.SEMAPHORE_VALUE_SUPPORTED:
    print(f"Current semaphore value: {sem.value}")
else:
    print("Semaphore value access not supported on this platform (e.g., macOS)")
    # Attempting to access sem.value would raise AttributeError

sem.close()
sem.unlink()

Working with System Limits

Message Queue Limits

The default values for message queue parameters may be quite restrictive on some systems:

import posix_ipc

print(f"Default max messages: {posix_ipc.QUEUE_MESSAGES_MAX_DEFAULT}")
print(f"Default max message size: {posix_ipc.QUEUE_MESSAGE_SIZE_MAX_DEFAULT}")
print(f"Max priority: {posix_ipc.QUEUE_PRIORITY_MAX}")

# Default max messages might be as low as 10 on Linux
# Consider providing explicit values for production use
mq = posix_ipc.MessageQueue('/prod_queue', posix_ipc.O_CREAT,
                           max_messages=100,           # Explicit value
                           max_message_size=8192)      # Explicit value

Deprecated Constants

Some constants are deprecated and should be replaced with standard library alternatives:

import posix_ipc
import os

# Deprecated (will be removed in future version)
# page_size = posix_ipc.PAGE_SIZE
# max_sem_value = posix_ipc.SEMAPHORE_VALUE_MAX

# Recommended alternatives
page_size = os.sysconf('SC_PAGE_SIZE')
max_sem_value = os.sysconf('SC_SEM_VALUE_MAX')

print(f"Page size: {page_size}")
print(f"Max semaphore value: {max_sem_value}")

Signal Range for Notifications

When using message queue notifications with signals, use the provided signal range:

import posix_ipc
import signal

# Check if realtime signals are available
if hasattr(posix_ipc, 'USER_SIGNAL_MIN'):
    # Use signal within the safe range
    notify_signal = posix_ipc.USER_SIGNAL_MIN + 1
    
    mq = posix_ipc.MessageQueue('/signal_queue', posix_ipc.O_CREAT)
    
    def signal_handler(signum, frame):
        print(f"Notification received on signal {signum}")
    
    signal.signal(notify_signal, signal_handler)
    mq.request_notification(notify_signal)
    
    # ... rest of application ...
else:
    print("Realtime signals not available")

Creation Flag Combinations

Common Flag Patterns

import posix_ipc

# Open existing object (fail if doesn't exist)
flags = 0

# Create if doesn't exist, open if exists  
flags = posix_ipc.O_CREAT

# Create new object (fail if already exists)
flags = posix_ipc.O_CREAT | posix_ipc.O_EXCL
# Or use the convenience constant:
flags = posix_ipc.O_CREX

# Create shared memory and truncate if exists (not supported on macOS)
flags = posix_ipc.O_CREAT | posix_ipc.O_TRUNC

Message Queue Access Modes

import posix_ipc

if posix_ipc.MESSAGE_QUEUES_SUPPORTED:
    # Read-only message queue
    mq_reader = posix_ipc.MessageQueue('/data_queue', read=True, write=False)
    
    # Write-only message queue  
    mq_writer = posix_ipc.MessageQueue('/data_queue', read=False, write=True)
    
    # Full access (default)
    mq_full = posix_ipc.MessageQueue('/data_queue', read=True, write=True)
    
    # Using flag constants (alternative approach)
    mq_readonly = posix_ipc.MessageQueue('/flag_queue', posix_ipc.O_CREAT | posix_ipc.O_RDONLY)

Version and Compatibility

Checking Module Version

import posix_ipc

print(f"posix_ipc version: {posix_ipc.VERSION}")
print(f"Author: {posix_ipc.__author__}")
print(f"License: {posix_ipc.__license__}")

# Version comparison for feature detection
version_parts = posix_ipc.VERSION.split('.')
major, minor = int(version_parts[0]), int(version_parts[1])

if (major, minor) >= (1, 3):
    print("Using modern version - PAGE_SIZE and SEMAPHORE_VALUE_MAX are deprecated")
else:
    print("Using older version - PAGE_SIZE and SEMAPHORE_VALUE_MAX still supported")

Comprehensive Platform Detection

import posix_ipc
import sys

def print_platform_support():
    print(f"Platform: {sys.platform}")
    print(f"posix_ipc version: {posix_ipc.VERSION}")
    print()
    print("Feature Support:")
    print(f"  Message Queues: {posix_ipc.MESSAGE_QUEUES_SUPPORTED}")
    print(f"  Semaphore Timeouts: {posix_ipc.SEMAPHORE_TIMEOUT_SUPPORTED}")
    print(f"  Semaphore Values: {posix_ipc.SEMAPHORE_VALUE_SUPPORTED}")
    print()
    print("System Limits:")
    print(f"  Default Max Messages: {posix_ipc.QUEUE_MESSAGES_MAX_DEFAULT}")
    print(f"  Default Max Message Size: {posix_ipc.QUEUE_MESSAGE_SIZE_MAX_DEFAULT}")
    print(f"  Max Message Priority: {posix_ipc.QUEUE_PRIORITY_MAX}")
    
    if hasattr(posix_ipc, 'USER_SIGNAL_MIN'):
        print(f"  User Signal Range: {posix_ipc.USER_SIGNAL_MIN}-{posix_ipc.USER_SIGNAL_MAX}")

print_platform_support()

Install with Tessl CLI

npx tessl i tessl/pypi-posix-ipc

docs

constants.md

index.md

message-queues.md

semaphores.md

shared-memory.md

tile.json