PythiloLesson 7 of 12 · progress kept in this browser

Share one in-flight computation per key

A cache miss under concurrent traffic can trigger many identical requests. Single-flight coordination shares the in-flight Task for a key. Completed or failed tasks are removed so a later call can retry. This is different from storing a permanent result cache.

One caller cancelling its wait should not automatically cancel work still needed by another caller. Shield protects that shared task from this propagation. The owner still needs a lifetime policy for work that outlives all callers. Tests use Events and checkpoints to measure actual call counts rather than timing guesses.

import asyncio
async def main():
    task=asyncio.create_task(asyncio.sleep(0,7))
    values=await asyncio.gather(task,task)
    print(values)
asyncio.run(main())
Your task

Implement SingleFlight with async get(key,factory), sharing one running factory(key) task per key. Remove finished tasks, and shield shared work from individual waiter cancellation.