Skip to content

Commit 3c1a084

Browse files
crazyengDorian Birraux
authored andcommitted
simplify asyncio
moving function later reusing the same implementation removing boilerplate code for run_in_loop need to use internal run using coro for consistency removing version checks
1 parent 3749e7e commit 3c1a084

1 file changed

Lines changed: 9 additions & 22 deletions

File tree

wolframclient/utils/asyncio.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
from __future__ import absolute_import, print_function, unicode_literals
22

33
import asyncio
4-
import functools
54

6-
7-
def run_in_loop(cor, loop=None):
8-
@functools.wraps(cor)
9-
def wrapped(*args, **kwargs):
10-
return get_event_loop(loop).run_until_complete(cor(*args, **kwargs))
11-
12-
return wrapped
5+
from wolframclient.utils.decorators import decorate
136

147

158
def get_event_loop(loop=None):
@@ -21,21 +14,15 @@ def get_event_loop(loop=None):
2114
return loop
2215

2316

24-
if hasattr(asyncio, "run"):
25-
run = asyncio.run
26-
else:
17+
def run(coro):
18+
return get_event_loop().run_until_complete(coro)
2719

28-
def run(main):
29-
loop = get_event_loop()
30-
return loop.run_until_complete(main)
3120

21+
run_in_loop = decorate(run)
3222

33-
if hasattr(asyncio, "create_task"):
34-
create_task = asyncio.create_task
35-
else:
3623

37-
def create_task(coro):
38-
""" ensure_future using get_event_loop, so that it behaves similarly to
39-
create_task, and gets the same signature.
40-
"""
41-
return asyncio.ensure_future(coro, loop=asyncio.get_event_loop())
24+
def create_task(coro):
25+
""" ensure_future using get_event_loop, so that it behaves similarly to
26+
create_task, and gets the same signature.
27+
"""
28+
return asyncio.ensure_future(coro, loop=get_event_loop())

0 commit comments

Comments
 (0)