Skip to main content

Named Locks to acquire lock by name

Project description

NamedLocks


NamedLocks is a simple library for locking on a name.

Installation

pip install namedlocks

Usage

import asyncio
from random import randint, uniform

from named_locks import AsyncNamedLock

NAMED_LOCKS = AsyncNamedLock()  # Create a global instance of the lock


async def task(uid: int):
    async with NAMED_LOCKS.lock(uid):  # Lock on some name
        print(f"Task {uid} started")
        # do something
        await asyncio.sleep(uniform(1, 5))
        print(f"Task {uid} finished")


async def main():
    tasks = [asyncio.create_task(task(uid)) for uid in [randint(1, 10) for _ in range(50)]]
    await asyncio.gather(*tasks)


asyncio.run(main())

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

namedlocks-0.0.1a3.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

namedlocks-0.0.1a3-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

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