Skip to content

Commit

Permalink
Add AsyncDecoratorBinder.asyncio() (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkang-quora committed Oct 27, 2023
1 parent cc3567e commit 7aec224
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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)

0 comments on commit 7aec224

Please sign in to comment.