Skip to content

Commit

Permalink
Fix DeduplicateDecorator.asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
dkang-quora committed Nov 15, 2023
1 parent b6abc2d commit f8791bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
23 changes: 22 additions & 1 deletion asynq/tests/test_asynq_to_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import asynq
from asynq import ConstFuture
from asynq.tools import AsyncTimer
from asynq.tools import AsyncTimer, deduplicate


def test_asyncio():
Expand Down Expand Up @@ -132,3 +132,24 @@ def j(x):
assert j(-100) == 788
assert j.asynq(-200).value() == 688
assert asyncio.run(j.asyncio(-300)) == 699


def test_deduplicate():
@deduplicate()
@asynq.asynq()
def l():
return 3

async def n():
return 4

@deduplicate()
@asynq.asynq(asyncio_fn=n)
def m():
return 3

assert l() == 3
assert asyncio.run(l.asyncio()) == 3

assert m() == 3
assert asyncio.run(m.asyncio()) == 4
27 changes: 16 additions & 11 deletions asynq/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@
"""

from .asynq_to_async import is_asyncio_mode
from .contexts import AsyncContext
from .decorators import asynq, async_call, AsyncDecorator, AsyncDecoratorBinder

from qcore import get_original_fn, utime
from qcore.caching import get_args_tuple, get_kwargs_defaults, LRUCache
from qcore.events import EventHook
from qcore.errors import reraise, prepare_for_reraise
from qcore.decorators import decorate
import functools
import inspect
import itertools
import weakref
import threading
import time
import weakref
from typing import Any, Awaitable

from qcore import get_original_fn, utime
from qcore.caching import LRUCache, get_args_tuple, get_kwargs_defaults
from qcore.decorators import decorate
from qcore.errors import prepare_for_reraise, reraise
from qcore.events import EventHook

from .asynq_to_async import is_asyncio_mode
from .contexts import AsyncContext
from .decorators import AsyncDecorator, AsyncDecoratorBinder, async_call, asynq


@asynq()
Expand Down Expand Up @@ -346,10 +348,13 @@ def __init__(self, fn, task_cls, keygetter):

def cache_key(self, args, kwargs):
return self.keygetter(args, kwargs), threading.current_thread(), id(self.fn)

def asyncio(self, *args, **kwargs) -> Awaitable[Any]:
return self.fn.asyncio(*args, **kwargs)

def asynq(self, *args, **kwargs):
if is_asyncio_mode():
return self.asyncio(*args, **kwargs)
return self.fn.asyncio(*args, **kwargs)

cache_key = self.cache_key(args, kwargs)

Expand Down

0 comments on commit f8791bd

Please sign in to comment.