A flexible low-level tool to make synchronisation primitives in asyncio Python
Project description
fifolock
A flexible low-level tool to make synchronisation primitives in asyncio Python. As the name suggests, locks are granted strictly in the order requested: first-in-first-out.
Installation
pip install fifolock
Recipes
Mutex (exclusive) lock
import asyncio
from fifolock import FifoLock
class Mutex(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Mutex]
lock = FifoLock()
async def access():
async with lock(Mutex):
# access resource
Read/write (shared/exclusive) lock
import asyncio
from fifolock import FifoLock
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
class Write(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Read] and not holds[Write]
lock = FifoLock()
async def read():
async with lock(Read):
# shared access
async def write():
async with lock(Write):
# exclusive access
Semaphore
import asyncio
from fifolock import FifoLock
class SemaphoreBase(asyncio.Future):
@classmethod
def is_compatible(cls, holds):
return holds[cls] < cls.size
lock = FifoLock()
Semaphore = type('Semaphore', (SemaphoreBase, ), {'size': 3})
async def access():
async with lock(Semaphore):
# at most 3 concurrent accesses
Running tests
python setup.py test
Design choices
Each mode of the lock is a subclass of asyncio.Future
. This could be seen as a leak some of the internals of FifoLock
, but it allows for clear client and internal code.
-
Classes are hashable, so each can be a key in the
holds
dictionary passed to theis_compatible
method. This allows the compatibility conditions to be read clearly in the client code, and theholds
dictionary to be mutated clearly internally. -
An instance of it, created inside
FifoLock
, is both the object awaited upon, and stored in a deque with a way of accessing itsis_compatible
method. -
The fact it's a class and not an instance of a class also makes clear it is to store no state, merely configuration.
A downside is that for configurable modes, such as for a semaphore, the client must dynamically create a class: this is not a frequently-used pattern.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fifolock-0.0.6.tar.gz
.
File metadata
- Download URL: fifolock-0.0.6.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91742b40ac34b18cec3fc174dfe3d94844f863e11fe3bf891ef07c590b111f14 |
|
MD5 | 07f16ae4b62a628630e8166f87524e16 |
|
BLAKE2b-256 | dba28727ce035881459fa7a9888e8e9a956b48ac2a2bbfb586eff894c2d7fbe6 |
File details
Details for the file fifolock-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: fifolock-0.0.6-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50e6ef1d565d11102006913db58ff56baf14de1273670dfbe60829b2bc4ba810 |
|
MD5 | a93e5ddbeab3b935984617753b78d32b |
|
BLAKE2b-256 | 3edd6bc3226ac25a87db1e3c0c4e1bd6a865ce8ea47fd2249d17f8970e5f81c0 |