New submission from pfreixes:

Currently, there is no way to access to the number of waiters pending to be 
woken up. This information can be useful for those environments which create 
and delete asyncio primitives instances depending if there are waiters still to 
be processed.

The following example shows an example of the DogPile solution that uses the 
Event lock mechanism. Each time that there is a miss in the cache, a new Event 
is created and it will be removed by the last waiter.


import asyncio

cache = {}
events = {}

async def get(process, key):
    try:
        return cache[key]
    except KeyError:
        try:
            await events[key].wait()
            if len(events[key]._waiters) == 0:
                events.pop(key)
            return cache[key]
        except KeyError:
            events[key] = asyncio.Event()
            # simulates some IO to get the Key
            await asyncio.sleep(0.1)
            cache[key] = "some random value"
            events[key].set()


async def main():
    tasks = [get(i, "foo") for i in range(1, 10)]
    await asyncio.gather(*tasks)

asyncio.get_event_loop().run_until_complete(main())

----------
components: asyncio
messages: 294357
nosy: pfreixes, yselivanov
priority: normal
severity: normal
status: open
title: Allow retrieve the number of waiters pending for most of the asyncio 
lock primitives
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30457>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to