-
Notifications
You must be signed in to change notification settings - Fork 0
/
tworoutine.py
executable file
·49 lines (32 loc) · 1.16 KB
/
tworoutine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python3
"""
Double-entry style asynchronous coding.
"""
import nest_asyncio
import asyncio
__all__ = ["tworoutine"]
nest_asyncio.apply()
class tworoutine(object):
instance = None # starts out unbound
def __init__(self, coroutine, instance=None):
self.__coroutine = coroutine
self.instance = instance
def __get__(self, obj, type_):
"""Descriptor allowing us to behave as bound or unbound methods."""
if obj is None:
return self # Unbound
return self.__class__(self.__coroutine.__get__(obj, type_))
def __call__(self, *args, **kwargs):
"""Stub for ordinary, serial call"""
# By default, presume a fancy asynchronous version has been coded and
# invoke it synchronously. This is often all the serial version needs
# to do anyways.
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
coroutine = (~self)(*args, **kwargs)
return loop.run_until_complete(coroutine)
def __invert__(self):
return self.__coroutine
# vim: sts=4 ts=4 sw=4 tw=78 smarttab expandtab