Skip to main content

lock decorators

Project description

This package provides decorators that will try to acquire lock before calling decorated function.

Installation

pip install lockorator

Usage

This package provides two flavours of lock decorators: redis and asyncio. Both flavours have the same api.

Package lockorator.asyncio provides asyncio lock decorators, also compatible with trio.

Package lockorator.redis provides redis lock decorators.

To use redis locks, set LOCKORATOR_REDIS_URL in your environment:

export LOCKORATOR_REDIS_URL="redis://localhost:6379"

API

lock_or_exit

Decorator. Before decorated function starts, try to acquire lock with specified identifier. If lock is acquired successfully, proceed executing the function. Otherwise, return immediately. The id argument can contain templated string, wich will be rendered with args and kwargs, passed to the function.

Example:

from lockorator.asyncio import lock_or_exit

@lock_or_exit('lock_work_{}')
def workwork(x):
    pass

workwork(3)  # Will try to acquire lock 'lock_work_3'

lock_wait

Decorator. Before decorated function starts, try to acquire lock with specified identifier, waiting for waittime seconds if needed. If lock is acquired successfully, proceed executing the function. Otherwise, raise lockorator.TimeoutError. The id argument can contain templated string, wich will be rendered with args and kwargs, passed to the function.

Example:

from lockorator.redis import lock_wait

@lock_wait('lock_work_{}', waittime=4)
def workwork(x):
    pass

workwork(3)  # Will try to acquire lock 'lock_work_3' for 4 seconds

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

lockorator-0.1.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

lockorator-0.1-py3-none-any.whl (4.9 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