Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AsyncAndSyncPairDecorator.__init__() #140

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions asynq/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ def __call__(self, *args, **kwargs):
class AsyncAndSyncPairDecorator(AsyncDecorator):
binder_cls = AsyncAndSyncPairDecoratorBinder

def __init__(self, fn, cls, sync_fn, kwargs={}):
AsyncDecorator.__init__(self, fn, cls, kwargs)
def __init__(self, fn, cls, sync_fn, kwargs={}, asyncio_fn=None):
AsyncDecorator.__init__(self, fn, cls, kwargs, asyncio_fn)
self.sync_fn = sync_fn

def __call__(self, *args, **kwargs):
if is_asyncio_mode():
raise RuntimeError("asyncio mode does not support synchronous calls")
return self.sync_fn(*args, **kwargs)

def __get__(self, owner, cls):
Expand All @@ -253,7 +255,11 @@ def __get__(self, owner, cls):
if self.type in (staticmethod, classmethod):
fn = self.type(fn)
new_self = qcore.decorators.decorate(
AsyncAndSyncPairDecorator, self.task_cls, sync_fn, self.kwargs
AsyncAndSyncPairDecorator,
self.task_cls,
sync_fn,
self.kwargs,
self.asyncio_fn,
)(fn)
return AsyncDecorator.__get__(new_self, owner, cls)

Expand Down Expand Up @@ -315,9 +321,9 @@ def decorate(fn):
)(fn)
return decorated
else:
return qcore.decorators.decorate(AsyncAndSyncPairDecorator, cls, sync_fn)(
fn
)
return qcore.decorators.decorate(
AsyncAndSyncPairDecorator, cls, sync_fn, kwargs, asyncio_fn
)(fn)

return decorate

Expand Down
12 changes: 11 additions & 1 deletion asynq/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def sync_fn():
return "sync_fn"


@asynq(sync_fn=sync_fn)
async def asyncio_fn():
return "asyncio_fn"


@asynq(sync_fn=sync_fn, asyncio_fn=asyncio_fn)
def async_fn():
return "async_fn"

Expand Down Expand Up @@ -239,6 +243,12 @@ def test_method_sync_fn():
assert_eq("async_method", instance.async_method.asynq().value())


def test_function_sync_asyncio_fn():
assert_eq("sync_fn", async_fn())
assert_eq("async_fn", async_fn.asynq().value())
assert_eq("asyncio_fn", asyncio.run(async_fn.asyncio()))


def test_pickling():
pickled = pickle.dumps(async_fn)
unpickled = pickle.loads(pickled)
Expand Down
Loading