Skip to main content

A flexible low-level tool to make synchronisation primitives in asyncio Python

Project description

fifolock CircleCI Maintainability Test Coverage

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; and are not reentrant.

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 the is_compatible method. This allows the compatibility conditions to be read clearly in the client code, and the holds 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 its is_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.

The fact that the lock is not reentrant is deliberate: the class of algorithms this is designed for does not require this. This would add unnecessary complexity, and presumably be slower.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fifolock-0.0.13.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

fifolock-0.0.13-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file fifolock-0.0.13.tar.gz.

File metadata

  • Download URL: fifolock-0.0.13.tar.gz
  • Upload date:
  • Size: 2.8 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

Hashes for fifolock-0.0.13.tar.gz
Algorithm Hash digest
SHA256 5e89096be9831e9002c46166a3b67c15a2d90c4de5ed260682da80160a810539
MD5 8df80ca219261d699c5323848b38c11a
BLAKE2b-256 3ed71bcf58ff4d693576e2930acb641a98076672e3b3aca7d62f83abcf56777c

See more details on using hashes here.

File details

Details for the file fifolock-0.0.13-py3-none-any.whl.

File metadata

  • Download URL: fifolock-0.0.13-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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

Hashes for fifolock-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f57b99612a0a80fc093a3d23c1079d954ecca6a2cf728b883f8ed8825449928b
MD5 47d0a482a25ac455464519a614b0bac8
BLAKE2b-256 beefb30850e6f844de2f1118d62bf39968bf745258e1b4fa4a0580c499c3ef9c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page