question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is my method of use wrong?

See original GitHub issue
import  asyncio
from aioredlock import Aioredlock, LockError, Sentinel
redis_instances = [('10.5.208.20',6379)]
lock_manager = Aioredlock(redis_instances)
lock_manager.retry_count = 10
async def get_lock():
    try:
        async with await lock_manager.lock("resource_name") as lock:
            print(lock.valid)
    except LockError as e:
        print("Lock not acquired")

async def main():
    tasks = [asyncio.create_task(get_lock()) for i in range(10)]
    await asyncio.wait(tasks)

asyncio.run(main())

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
centosredhatcommented, Apr 11, 2021

Thanks.

0reactions
gtmanfredcommented, Apr 11, 2021

No problem If you want to reuse the connection, you can instantiate a connection pool from aioredis first, and then pass that in as your instance, and that will allow you to reuse the connection pool.

import  asyncio

from aioredis import create_redis_pool
from aioredlock import Aioredlock, LockError

redis_instance = ('172.17.0.2', 6379)


class Locker:
    _conn = None
    _manager = None

    def __new__(cls, conn_str):
        cls.conn_str = conn_str
        return super().__new__(cls)

    @classmethod
    async def _get_conn(cls):
        if cls._conn is None:
            cls._conn = await create_redis_pool(cls.conn_str)
        return cls._conn

    async def get_manager(self):
        if self._manager is None:
            lock = await Locker._get_conn()
            self._manager = Aioredlock([await Locker._get_conn()])
            self._manager.retry_count = 10
        return self._manager

    async def get_lock(self):
        manager = await self.get_manager()
        try:
            async with await manager.lock("resource_name") as lock:
                print(lock.valid)
        except LockError as e:
            print("Lock not acquired")


async def main():
    tasks = [asyncio.create_task(Locker(redis_instance).get_lock()) for i in range(10)]
    await asyncio.wait(tasks)


asyncio.run(main())

This isn’t the best example, because it kind of just depends on class properties to make sure they don’t get used a second time. But it works as an example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Beginner - What is wrong with my method?
In java you don't declare methods in methods. Change that to public String critMeth(String name){ String c = name + " loves you!...
Read more >
using the wrong method | English examples in context
High quality example sentences with “using the wrong method” in context from reliable sources - Ludwig is the linguistic search engine that helps...
Read more >
Error of the method: what is it for? - PMC - NCBI
The casual error, also known as random error, occurs due to the difficulty and/or inaccuracy in either identifying or defining certain points.
Read more >
PDE IVP - Characteristics, why is my method wrong?
Using the initial condition, we find that A(x)=sinx so that u(x,t)=t+sinx. But this is not the correct solution and I have not used...
Read more >
9 Words and Phrases You're Probably Using Wrong
Here's a primer on how to use (or not use) nine words and phrases common in ... Sure, saying the wrong word (usually)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found