All Projects → mattrasband → aioredis-lock

mattrasband / aioredis-lock

Licence: MIT license
Distributed locking implementation for aioredis

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to aioredis-lock

aioredis-cluster
Redis Cluster support extension for aioredis
Stars: ✭ 21 (+5%)
Mutual labels:  asyncio, aioredis
Aioredis Py
asyncio (PEP 3156) Redis support
Stars: ✭ 2,003 (+9915%)
Mutual labels:  asyncio, aioredis
bolsa
Biblioteca feita em Python com o objetivo de facilitar o acesso a dados de seus investimentos na bolsa de valores(B3/CEI) através do Portal CEI.
Stars: ✭ 46 (+130%)
Mutual labels:  asyncio
callosum
An RPC Transport Library for asyncio
Stars: ✭ 17 (-15%)
Mutual labels:  asyncio
asyncio-throttle
Simple, easy-to-use throttler for asyncio.
Stars: ✭ 95 (+375%)
Mutual labels:  asyncio
duckpy
A simple Python library for searching on DuckDuckGo.
Stars: ✭ 20 (+0%)
Mutual labels:  asyncio
pydf
PDF generation in python using wkhtmltopdf for heroku and docker
Stars: ✭ 68 (+240%)
Mutual labels:  asyncio
hikari
A Discord API wrapper for Python and asyncio built on good intentions.
Stars: ✭ 631 (+3055%)
Mutual labels:  asyncio
showdown-battle-bot
Socket Battle Bot for Pokemon Showdown (http://pokemonshowdown.com/)
Stars: ✭ 19 (-5%)
Mutual labels:  asyncio
pyaiot
A set of Python services to interact and transport data from IoT devices
Stars: ✭ 29 (+45%)
Mutual labels:  asyncio
py3tftp
An asynchronous TFTP server in pure Python 3.5
Stars: ✭ 39 (+95%)
Mutual labels:  asyncio
pglock
PostgreSQL Lock Client for Go
Stars: ✭ 50 (+150%)
Mutual labels:  locking
python-logi-circle
Python 3.6+ API for Logi Circle cameras
Stars: ✭ 23 (+15%)
Mutual labels:  asyncio
spinach
Modern Redis task queue for Python 3
Stars: ✭ 46 (+130%)
Mutual labels:  asyncio
python-socks
Core proxy client (SOCKS4, SOCKS5, HTTP) functionality for Python
Stars: ✭ 40 (+100%)
Mutual labels:  asyncio
async armor
Graceful drop-in replacement for asyncio.shield
Stars: ✭ 15 (-25%)
Mutual labels:  asyncio
pytest-aiohttp
pytest plugin for aiohttp support
Stars: ✭ 110 (+450%)
Mutual labels:  asyncio
tomodachi
💻 Microservice library / framework using Python's asyncio event loop with full support for HTTP + WebSockets, AWS SNS+SQS, RabbitMQ / AMQP, middleware, etc. Extendable for GraphQL, protobuf, gRPC, among other technologies.
Stars: ✭ 170 (+750%)
Mutual labels:  asyncio
netunnel
A tool to create network tunnels over HTTP/S written in Python 3
Stars: ✭ 19 (-5%)
Mutual labels:  asyncio
python3-concurrency
Python3爬虫系列的理论验证,首先研究I/O模型,分别用Python实现了blocking I/O、nonblocking I/O、I/O multiplexing各模型下的TCP服务端和客户端。然后,研究同步I/O操作(依序下载、多进程并发、多线程并发)和异步I/O(asyncio)之间的效率差别
Stars: ✭ 49 (+145%)
Mutual labels:  asyncio

aioredis_lock

CircleCI

Implementation of distributed locking with aioredis, an asyncio based redis client.

This is a standalone lib until, and if, aio-libs/aioredis#573 is accepted.

Usage

You need an aioredis.RedisConnection or aioredis.ConnectionsPool already created.

Mutex

from aioredis_lock import RedisLock, LockTimeoutError

try:
    async with RedisLock(
        pool,
        key="foobar",
        # how long until the lock should expire (seconds). this can be extended
        # via `await lock.extend(30)`
        timeout=30,
        # you can customize how long to allow the lock acquisitions to be
        # attempted.
        wait_timeout=30,
    ) as lock:
        # If you get here, you now have a lock and are the only program that
        # should be running this code at this moment.

        # do some work...

        # we may want it longer...
        await lock.extend(30)

except LockTimeoutError:
    # The lock could not be acquired by this worker and we should give up
    pass

Simple Leader/Follower(s)

Let's suppose you need a simple leader/follower type implementation where you have a number of web-workers but just want 1 to preform a repeated task. In the case the leader fails someone else should pick up the work. Simply pass wait_timeout=None to RedisLock allowing the worker to keep trying to get a lock for when the leader eventually fails. The main complication here is extending the lock and validating the leader still owns it.

from aioredis_lock import RedisLock

# if the lock is lost, we still want to be a follower
while True:

    # wait indefinitely to acquire a lock
    async with RedisLock(pool, "shared_key", wait_timeout=None) as lock:

        # hold the lock as long as possible
        while True:
            if not await lock.is_owner():
                logger.debug("We are no longer the lock owner, falling back")
                break

            # do some work

            if not await lock.renew():
                logger.debug("We lost the lock, falling back to follower mode")
                break

This mostly delegates the work of selecting and more importantly promoting leaders.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].