System V IPC primitives for Python. Raw syscalls, thin wrappers, no ceremony.
sysvipc exposes the classic AT&T System V inter-process communication calls to
Python: semaphore sets (semget, semop, semctl),
message queues (msgget, msgsnd, msgrcv, msgctl),
and shared memory (shmget, shmat, shmctl).
Failures raise OSError straight from the kernel errno.
Why System V in the age of POSIX IPC? Because of the rawness. One semop call can
increment or decrement a semaphore by any value, operate atomically on a whole set, and the
SEM_UNDO flag asks the kernel to revert your operations when the process dies for
any reason. Message queues are addressable by type, shared memory is just memory.
pip install sysvipc
pip install sysvipc
# Semaphores: acquire and release, kernel-managed cleanup
sem = sysvipc.semget(sysvipc.IPC_PRIVATE, 1, 0o600)
atexit.register(lambda: sysvipc.semctl(sem, 0, sysvipc.IPC_RMID))
sysvipc.semop(sem, [(0, 1, sysvipc.SEM_UNDO)]) # release
curval = sysvipc.semctl(sem, 0, sysvipc.GETVAL)
sysvipc.semop(sem, [(0, -1, sysvipc.SEM_UNDO)]) # acquire
# Message queues: typed messages
msqid = sysvipc.msgget(sysvipc.IPC_PRIVATE, 0o600)
sysvipc.msgsnd(msqid, 1, "hello")
mtype, message = sysvipc.msgrcv(msqid, 0, 1024)
# Shared memory: plain memoryview, plain bytes
shmid = sysvipc.shmget(sysvipc.IPC_PRIVATE, 4096, 0o600)
with sysvipc.shmat(shmid) as mem:
memoryview(mem)[0:5] = b"hello"
| Name | Meaning |
|---|---|
IPC_PRIVATE | Always create a fresh, unkeyed resource. |
IPC_CREAT / IPC_EXCL | Create if missing / fail if it already exists. |
IPC_RMID | Remove the resource immediately. |
IPC_STAT / IPC_SET | Read / write kernel status as a dict. |
IPC_NOWAIT | Fail with EAGAIN or ENOMSG instead of blocking. |
SEM_UNDO | Kernel undoes the operation when the process terminates. The pearl of System V IPC. |
GETVAL / SETVAL | Read / set one semaphore value. |
GETALL / SETALL | Read / set all semaphore values in the set. |
GETPID / GETNCNT / GETZCNT | Last operating pid, waiters for increase, waiters for zero. |
MSG_NOERROR | Truncate overlong messages instead of failing with E2BIG. |
SHM_RDONLY | Attach shared memory for reading only. |
semflg/msgflg/shmflg mode bits, cleanup is your atexit handler calling IPC_RMID.semop, semtimedop, msgsnd, msgrcv) drop the GIL so other Python threads keep running.