Skip to content

Commit 7809307

Browse files
Merge pull request hugapi#314 from timothycrosley/feature/nested_asyncio_support
Feature/nested asyncio support
2 parents 689f499 + f1435c7 commit 7809307

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Ideally, within a virtual environment.
1111

1212
Changelog
1313
=========
14+
### 2.1.3
15+
- Fixed nested async calls so that they reuse the same loop
16+
1417
### 2.1.2
1518
- Fixed an issue with sharing exception handlers accross multiple modules (Thanks @soloman1124)
1619
- Fixed how single direction (response / request) middlewares are bounded to work when code is Cython compiled

hug/interface.py

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

5050
def asyncio_call(function, *args, **kwargs):
5151
loop = asyncio.get_event_loop()
52+
if loop.is_running():
53+
return function(*args, **kwargs)
54+
5255
function = ensure_future(function(*args, **kwargs), loop=loop)
5356
loop.run_until_complete(function)
5457
return function.result()

tests/test_async.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@ async def hello_world():
3737
assert loop.run_until_complete(hello_world()) == "Hello World!"
3838

3939

40-
def test_basic_call_on_method_async():
40+
def tested_nested_basic_call_async():
41+
"""Test to ensure the most basic call still works if applied to a method"""
42+
@hug.call()
43+
async def hello_world(self=None):
44+
return await nested_hello_world()
45+
46+
@hug.local()
47+
async def nested_hello_world(self=None):
48+
return "Hello World!"
49+
50+
assert hello_world.interface.http
51+
assert loop.run_until_complete(hello_world()) == "Hello World!"
52+
assert hug.test.get(api, '/hello_world').data == "Hello World!"
53+
4154

55+
def test_basic_call_on_method_async():
4256
"""Test to ensure the most basic call still works if applied to a method"""
4357
class API(object):
4458

@@ -53,7 +67,7 @@ async def hello_world(self=None):
5367

5468

5569
def test_basic_call_on_method_through_api_instance_async():
56-
70+
"""Test to ensure instance method calling via async works as expected"""
5771
class API(object):
5872

5973
def hello_world(self):
@@ -70,7 +84,7 @@ async def hello_world():
7084

7185

7286
def test_basic_call_on_method_registering_without_decorator_async():
73-
87+
"""Test to ensure async methods can be used without decorator"""
7488
class API(object):
7589

7690
def __init__(self):
@@ -83,3 +97,5 @@ async def hello_world_method(self):
8397

8498
assert loop.run_until_complete(api_instance.hello_world_method()) == "Hello World!"
8599
assert hug.test.get(api, '/hello_world_method').data == "Hello World!"
100+
101+

tests/test_coroutines.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
def test_basic_call_coroutine():
31-
""" The most basic Happy-Path test for Hug APIs using async """
31+
"""The most basic Happy-Path test for Hug APIs using async"""
3232
@hug.call()
3333
@asyncio.coroutine
3434
def hello_world():
@@ -37,8 +37,22 @@ def hello_world():
3737
assert loop.run_until_complete(hello_world()) == "Hello World!"
3838

3939

40-
def test_basic_call_on_method_coroutine():
40+
def test_nested_basic_call_coroutine():
41+
"""The most basic Happy-Path test for Hug APIs using async"""
42+
@hug.call()
43+
@asyncio.coroutine
44+
def hello_world():
45+
return asyncio.async(nested_hello_world())
46+
47+
@hug.local()
48+
@asyncio.coroutine
49+
def nested_hello_world():
50+
return "Hello World!"
4151

52+
assert loop.run_until_complete(hello_world()) == "Hello World!"
53+
54+
55+
def test_basic_call_on_method_coroutine():
4256
"""Test to ensure the most basic call still works if applied to a method"""
4357
class API(object):
4458

@@ -54,7 +68,7 @@ def hello_world(self=None):
5468

5569

5670
def test_basic_call_on_method_through_api_instance_coroutine():
57-
71+
"""Test to ensure the most basic call still works if applied to a method"""
5872
class API(object):
5973

6074
def hello_world(self):
@@ -72,7 +86,7 @@ def hello_world():
7286

7387

7488
def test_basic_call_on_method_registering_without_decorator_coroutine():
75-
89+
"""Test to ensure instance method calling via async works as expected"""
7690
class API(object):
7791

7892
def __init__(self):

0 commit comments

Comments
 (0)