[New-bugs-announce] [issue30457] Allow retrieve the number of waiters pending for most of the asyncio lock primitives
pfreixes
report at bugs.python.org
Wed May 24 10:48:25 EDT 2017
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 <report at bugs.python.org>
<http://bugs.python.org/issue30457>
_______________________________________
More information about the New-bugs-announce
mailing list