Skip to content

Commit 96445e5

Browse files
committed
fix: future exception never retrived problem (#144)
1 parent c02c3b6 commit 96445e5

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

cashews/decorators/locked.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,21 @@ async def _wrap(*args, **kwargs):
7777

7878

7979
def thunder_protection(key: Optional[KeyOrTemplate] = None) -> Decorator:
80-
futures: Dict[str, asyncio.Future] = {}
80+
tasks: Dict[str, asyncio.Task] = {}
8181

8282
def _decor(func: AsyncCallable_T) -> AsyncCallable_T:
8383
_key_template = get_cache_key_template(func, key=key)
8484

85-
def done_callback(_key: Key, task: asyncio.Task):
86-
future = futures[_key]
87-
if task.exception():
88-
future.set_exception(task.exception())
89-
else:
90-
future.set_result(task.result())
91-
del futures[_key]
85+
def done_callback(_key: Key, _: asyncio.Task):
86+
del tasks[_key]
9287

9388
@wraps(func)
9489
async def _wrapper(*args, **kwargs):
9590
_key = get_cache_key(func, _key_template, args, kwargs)
96-
if _key in futures:
97-
return await futures[_key]
91+
if _key in tasks:
92+
return await tasks[_key]
9893
task = asyncio.create_task(func(*args, **kwargs))
99-
futures[_key] = asyncio.Future()
94+
tasks[_key] = task
10095
task.add_done_callback(partial(done_callback, _key))
10196
return await task
10297

0 commit comments

Comments
 (0)