Skip to content

Commit 84e4f51

Browse files
Merge pull request #780 from hugapi/feature/python35-simplifications-async
Feature/python35 simplifications async
2 parents 41a14ca + d62d691 commit 84e4f51

File tree

8 files changed

+20
-64
lines changed

8 files changed

+20
-64
lines changed

CHANGELOG.md

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

1313
Changelog
1414
=========
15+
### 2.5.1 - TBD
16+
- Optimizations and simplification of async support, taking advantadge of Python3.4 deprecation.
17+
1518
### 2.5.0 - May 4, 2019
1619
- Updated to latest Falcon: 2.0.0
1720
- Added support for Marshmallow 3

hug/_async.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

hug/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"""
2222
from __future__ import absolute_import
2323

24+
import asyncio
2425
import sys
2526
from collections import OrderedDict, namedtuple
2627
from distutils.util import strtobool
@@ -35,7 +36,6 @@
3536
import hug.defaults
3637
import hug.output_format
3738
from hug import introspect
38-
from hug._async import asyncio, ensure_future
3939
from hug._version import current
4040

4141
INTRO = """

hug/interface.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from __future__ import absolute_import
2323

2424
import argparse
25+
import asyncio
2526
import os
2627
import sys
2728
from collections import OrderedDict
@@ -35,12 +36,21 @@
3536
import hug.output_format
3637
import hug.types as types
3738
from hug import introspect
38-
from hug._async import asyncio_call
3939
from hug.exceptions import InvalidTypeData
4040
from hug.format import parse_content_type
4141
from hug.types import MarshmallowInputSchema, MarshmallowReturnSchema, Multiple, OneOf, SmartBoolean, Text, text
4242

4343

44+
def asyncio_call(function, *args, **kwargs):
45+
loop = asyncio.get_event_loop()
46+
if loop.is_running():
47+
return function(*args, **kwargs)
48+
49+
function = asyncio.ensure_future(function(*args, **kwargs), loop=loop)
50+
loop.run_until_complete(function)
51+
return function.result()
52+
53+
4454
class Interfaces(object):
4555
"""Defines the per-function singleton applied to hugged functions defining common data needed by all interfaces"""
4656

hug/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from urllib.parse import urlencode
3030

3131
from falcon import HTTP_METHODS
32-
from falcon.testing import StartResponseMock, create_environ, DEFAULT_HOST
32+
from falcon.testing import DEFAULT_HOST, StartResponseMock, create_environ
3333

3434
from hug import output_format
3535
from hug.api import API

requirements/development.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Cython==0.29.6
33
-r common.txt
44
flake8==3.5.0
55
ipython==6.2.1
6-
isort==4.2.5
6+
isort==4.3.18
77
pytest-cov==2.5.1
88
pytest==4.3.1
99
python-coveralls==2.9.1

tests/test_decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
OTHER DEALINGS IN THE SOFTWARE.
2020
2121
"""
22+
import asyncio
2223
import json
2324
import os
2425
import sys
@@ -33,7 +34,6 @@
3334
from marshmallow import ValidationError
3435

3536
import hug
36-
from hug._async import coroutine
3737
from hug.exceptions import InvalidTypeData
3838

3939
from .constants import BASE_DIRECTORY
@@ -1476,7 +1476,7 @@ def happens_on_startup(api):
14761476
pass
14771477

14781478
@hug.startup()
1479-
@coroutine
1479+
@asyncio.coroutine
14801480
def async_happens_on_startup(api):
14811481
pass
14821482

tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from uuid import UUID
2727

2828
import pytest
29-
from marshmallow import Schema, fields, ValidationError
29+
from marshmallow import Schema, ValidationError, fields
3030
from marshmallow.decorators import validates_schema
3131

3232
import hug

0 commit comments

Comments
 (0)