[ ok ] sysvipc 1.1.0
[ ok ] semaphores, message queues, shared memory attached

sysvipc

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.

Install

pip install sysvipc

pip install sysvipc

Quick session

# 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"

Reference

Semaphores

semget (key, nsems, semflg) -> semid
Create or open a semaphore set.
semop (semid, sops) -> 0
Apply a list of (sem_num, sem_op, sem_flg) operations atomically. Released from the GIL while blocking.
semtimedop (semid, sops, timeout) -> 0
Like semop but gives up after timeout seconds with EAGAIN.
semctl (semid, semnum, cmd [, arg])
Control: IPC_RMID, IPC_STAT (dict), IPC_SET (dict), GETVAL, SETVAL (int), GETALL (list), SETALL (list), GETPID, GETNCNT, GETZCNT.

Message queues

msgget (key, msgflg) -> msqid
Create or open a queue.
msgsnd (msqid, mtype, message [, msgflg]) -> 0
Send bytes or str with a positive type.
msgrcv (msqid [, msgtyp [, msgsz [, msgflg]]]) -> (mtype, message)
Receive. msgtyp > 0 reads that type, msgtyp < 0 reads the lowest type <= abs(msgtyp), 0 reads the first message. Defaults to msgsz = msg_qbytes.
msgctl (msqid, cmd [, arg])
Control: IPC_RMID, IPC_STAT (dict), IPC_SET (dict).

Shared memory

shmget (key, size, shmflg) -> shmid
Allocate a segment.
shmat (shmid [, shmflg]) -> shm
Attach. Returns a buffer object: slice it, wrap it in memoryview, use it as a context manager. Detaches on exit or GC. SHM_RDONLY gives a read-only view.
shmctl (shmid, cmd [, arg])
Control: IPC_RMID, IPC_STAT (dict), IPC_SET (dict).

Flags

NameMeaning
IPC_PRIVATEAlways create a fresh, unkeyed resource.
IPC_CREAT / IPC_EXCLCreate if missing / fail if it already exists.
IPC_RMIDRemove the resource immediately.
IPC_STAT / IPC_SETRead / write kernel status as a dict.
IPC_NOWAITFail with EAGAIN or ENOMSG instead of blocking.
SEM_UNDOKernel undoes the operation when the process terminates. The pearl of System V IPC.
GETVAL / SETVALRead / set one semaphore value.
GETALL / SETALLRead / set all semaphore values in the set.
GETPID / GETNCNT / GETZCNTLast operating pid, waiters for increase, waiters for zero.
MSG_NOERRORTruncate overlong messages instead of failing with E2BIG.
SHM_RDONLYAttach shared memory for reading only.

Notes