Skip to content

Commit

Permalink
Fix async_proxy + .asyncio() (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkang-quora committed Nov 16, 2023
1 parent 2c696bc commit 406f29e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions asynq/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ def __init__(self, fn, asyncio_fn=None):
AsyncDecorator.__init__(self, fn, None, asyncio_fn=asyncio_fn)

def _call_pure(self, args, kwargs):
if asynq_to_async.is_asyncio_mode():
return self.asyncio(*args, **kwargs)

return self.fn(*args, **kwargs)


Expand Down
27 changes: 27 additions & 0 deletions asynq/tests/test_asynq_to_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,36 @@ async def k(x):
def j(x):
return ConstFuture(x + 888)

@asynq.asynq()
def jj(x):
return (yield j.asynq(x))

assert j(-100) == 788
assert j.asynq(-200).value() == 688
assert asyncio.run(j.asyncio(-300)) == 699
assert asyncio.run(jj.asyncio(0)) == 999
assert jj(0) == 888
assert jj.asynq(0).value() == 888


def test_proxy_and_bind():
async def async_g(self, x):
return x + 20 + B.SELF

class B:
SELF = 500

@asynq.async_proxy(asyncio_fn=async_g)
def g(self, x):
return ConstFuture(x + 10 + B.SELF)

@asynq.asynq()
def f(self, x):
return (yield self.g.asynq(x)) + 1

b = B()
assert b.f(0) == 511
assert asyncio.run(b.f.asyncio(1000)) == 1521


def test_deduplicate():
Expand Down

0 comments on commit 406f29e

Please sign in to comment.