Metadata-Version: 2.4
Name: shm_ipc_lock
Version: 0.1.0
Summary: Inter-process spinlock using shared memory and compare-and-swap implemented in C for Python.
Author-email: Sergiy Sanin <sanin_sergiy@yahoo.com>
License: MIT
Keywords: ipc,lock,shared-memory,spinlock,atomic,compare-and-swap
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: C
Classifier: Operating System :: POSIX :: Linux
Classifier: Topic :: System :: Operating System Kernels
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENCE
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: mypy>=1.0; extra == "dev"
Requires-Dist: black>=24.0; extra == "dev"
Requires-Dist: ruff>=0.6.0; extra == "dev"
Provides-Extra: examples
Requires-Dist: pytest; extra == "examples"
Dynamic: license-file

# A Simple Atomic CAS-Based Spin Lock for Python IPC 

Python's multiprocessing.shared_memory module, introduced in Python 3.8, provides a way to implement named shared memory for inter-process communication. It is somewhat analogous to the POSIX shared memory API.

However, Python's standard library does not directly support named semaphores based on the POSIX sem_open concept for inter-process synchronization by name. This is a very serious inconvenience when you work with shared memory and need to synchronize data access to this memory from multiple processes.

As one solution [pypi.org](pypi.org) has the following library: https://pypi.org/project/named_semaphores/

This repo is a very simplified version of just a spin lock, based on the compare and swap operation. It is using a 1 byte flag in shared memory at a given memory offset offest.

The need for something like this appeared when I was writing trading strategies that were somewhat pushing Python's performance boundaries. I needed to share the whole strategy's order cache (locally stored list of orders that are still open in the market) across processes of my market making strategy, each one serving it's own order book depth. I couldn't trust the standard Python IPC API because it is not designed for low-latency applications.
