{"id":9349,"library":"sysv-ipc","title":"SysV IPC","description":"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.","status":"active","version":"1.2.0","language":"en","source_language":"en","source_url":"https://github.com/osvenskan/sysv_ipc/","tags":["ipc","sysv","inter-process-communication","shared-memory","semaphores","message-queues"],"install":[{"cmd":"pip install sysv_ipc","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Semaphore","correct":"from sysv_ipc import Semaphore"},{"symbol":"SharedMemory","correct":"from sysv_ipc import SharedMemory"},{"symbol":"MessageQueue","correct":"from sysv_ipc import MessageQueue"},{"symbol":"IPC_PRIVATE","correct":"from sysv_ipc import IPC_PRIVATE"},{"symbol":"ftok","correct":"from sysv_ipc import ftok"}],"quickstart":{"code":"import sysv_ipc\nimport time\nimport os\n\n# IPC_PRIVATE creates a new unique semaphore set for demonstration.\n# For multi-process communication, use a shared key, e.g.,\n# key = sysv_ipc.ftok(os.getcwd(), 1)\n\ntry:\n    # Create a semaphore set with 1 semaphore, initial value 1.\n    # IPC_CREX ensures exclusive creation, raising an error if it exists.\n    sem = sysv_ipc.Semaphore(sysv_ipc.IPC_PRIVATE, flags=sysv_ipc.IPC_CREX, initial_value=1)\n    print(f\"Semaphore created with key: {sem.key}\")\n\n    print(\"Attempting to acquire semaphore...\")\n    sem.acquire() # Decrement semaphore value by 1\n    print(\"Semaphore acquired. Simulating work...\")\n    time.sleep(0.5)\n    sem.release() # Increment semaphore value by 1\n    print(\"Semaphore released.\")\n\n    # It's crucial to remove IPC objects after use to free system resources.\n    sem.remove()\n    print(\"Semaphore removed.\")\n\nexcept sysv_ipc.ExistentialError:\n    print(\"Semaphore with this key already exists. This quickstart uses IPC_PRIVATE to avoid conflicts.\")\n    print(\"For multi-process, use sysv_ipc.ftok(path, id) for a shared key and omit IPC_CREX.\")\nexcept sysv_ipc.Error as e:\n    print(f\"An IPC error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates the creation, acquisition, release, and removal of a semaphore. In a real multi-process application, processes would share a key (e.g., generated by `sysv_ipc.ftok`) to synchronize access to shared resources."},"warnings":[{"fix":"Ensure you are using Python 3. For Python 2, consider the `sysv_ipc_py2` fork or older `sysv-ipc` versions if absolutely necessary, but these are no longer maintained.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always call the `.remove()` method on IPC objects when they are no longer needed. In production, implement robust cleanup routines (e.g., signal handlers, daemon shutdown hooks).","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure the specified `path` exists and the process has read/execute permissions for it. Using `os.getcwd()` for simple demos is common, but in production, use a stable, known file path.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"When attaching to an existing shared memory segment, either omit the `size` parameter (which will infer it from the existing segment) or ensure the provided `size` exactly matches the size used during its creation.","message":"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`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Check 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`).","cause":"The current user or process lacks the necessary permissions to create, access, or remove SysV IPC objects on the system.","error":"OSError: [Errno 13] Permission denied"},{"fix":"For 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.","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.","error":"OSError: [Errno 22] Invalid argument"},{"fix":"If 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.","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.","error":"sysv_ipc.ExistentialError: [Errno 17] File exists"},{"fix":"The correct function name is `sysv_ipc.exists(key, type)`, not `exist`.","cause":"A common typo when trying to check if an IPC object exists.","error":"AttributeError: module 'sysv_ipc' has no attribute 'exist'"}]}