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())Review the design
The identity check prevents an old callback from deleting newer work for the same key. Retrieving a finished task exception prevents an abandoned shared task from producing an unobserved-exception warning.
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.
The explanation opens here once every check passes.