Skip to content

Commit ef01ea4

Browse files
Add initial support for asyncio nested calls
1 parent 5ed495d commit ef01ea4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

hug/interface.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
def asyncio_call(function, *args, **kwargs):
5151
loop = asyncio.get_event_loop()
52+
if loop.is_running():
53+
return await function(*args, **kwargs)
5254
function = ensure_future(function(*args, **kwargs), loop=loop)
5355
loop.run_until_complete(function)
5456
return function.result()

tests/test_async.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,38 @@ async def hello_world():
3838

3939

4040
def test_basic_call_on_method_async():
41+
"""Test to ensure the most basic call still works if applied to a method"""
42+
class API(object):
4143

44+
@hug.call()
45+
async def hello_world(self=None):
46+
return "Hello World!"
47+
48+
api_instance = API()
49+
assert api_instance.hello_world.interface.http
50+
assert loop.run_until_complete(api_instance.hello_world()) == "Hello World!"
51+
assert hug.test.get(api, '/hello_world').data == "Hello World!"
52+
53+
54+
def test_nested_call_on_method_async():
4255
"""Test to ensure the most basic call still works if applied to a method"""
4356
class API(object):
4457

4558
@hug.call()
4659
async def hello_world(self=None):
4760
return "Hello World!"
4861

62+
@hug.local()
63+
async def
64+
4965
api_instance = API()
5066
assert api_instance.hello_world.interface.http
5167
assert loop.run_until_complete(api_instance.hello_world()) == "Hello World!"
5268
assert hug.test.get(api, '/hello_world').data == "Hello World!"
5369

5470

5571
def test_basic_call_on_method_through_api_instance_async():
56-
72+
"""Test to ensure instance method calling via async works as expected"""
5773
class API(object):
5874

5975
def hello_world(self):
@@ -70,7 +86,7 @@ async def hello_world():
7086

7187

7288
def test_basic_call_on_method_registering_without_decorator_async():
73-
89+
"""Test to ensure async methods can be used without decorator"""
7490
class API(object):
7591

7692
def __init__(self):
@@ -83,3 +99,5 @@ async def hello_world_method(self):
8399

84100
assert loop.run_until_complete(api_instance.hello_world_method()) == "Hello World!"
85101
assert hug.test.get(api, '/hello_world_method').data == "Hello World!"
102+
103+

0 commit comments

Comments
 (0)