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

Add AsyncDecoratorBinder.asyncio() #129

Merged
merged 2 commits into from
Oct 27, 2023
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
6 changes: 6 additions & 0 deletions asynq/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ def asynq(self, *args, **kwargs):
else:
return self.decorator.asynq(self.instance, *args, **kwargs)

def asyncio(self, *args, **kwargs) -> Awaitable[Any]:
if self.instance is None:
return self.decorator.asyncio(*args, **kwargs)
else:
return self.decorator.asyncio(self.instance, *args, **kwargs)


class AsyncDecorator(PureAsyncDecorator):
binder_cls = AsyncDecoratorBinder
Expand Down
26 changes: 25 additions & 1 deletion asynq/tests/test_asynq_to_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import asyncio
import time

from qcore.asserts import assert_eq

import asynq
Expand All @@ -33,7 +34,7 @@ def f2():
def f(x):
a = yield asynq.ConstFuture(3)
b = yield asynq.ConstFuture(2)
assert (yield None) == None
assert (yield None) is None
return a - b + x

@asynq.asynq()
Expand Down Expand Up @@ -81,3 +82,26 @@ def f3():
assert_eq(400000, t1, tolerance=10000) # 400ms, 10us tolerance
assert_eq(200000, t2, tolerance=10000) # 200ms, 10us tolerance
assert_eq(000000, t3, tolerance=10000) # 0ms, 10us tolerance


def test_method():
async def g(slf, x):
return slf._x + x + 20

class A:
def __init__(self, x):
self._x = x

@asynq.asynq(asyncio_fn=g)
def f(self, x):
return self._x + x + 10

a = A(100)
assert_eq(a.f(5), 115)

@asynq.asynq()
def original(x):
return (yield a.f.asynq(x))

assert_eq(original(6), 116)
assert_eq(asyncio.run(a.f.asyncio(7)), 127)