Message294357
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()) |
|
Date |
User |
Action |
Args |
2017-05-24 14:48:25 | pfreixes | set | recipients:
+ pfreixes, yselivanov |
2017-05-24 14:48:25 | pfreixes | set | messageid: <1495637305.13.0.0526704297983.issue30457@psf.upfronthosting.co.za> |
2017-05-24 14:48:25 | pfreixes | link | issue30457 messages |
2017-05-24 14:48:25 | pfreixes | create | |
|