-
Notifications
You must be signed in to change notification settings - Fork 6
/
future.py
33 lines (23 loc) · 787 Bytes
/
future.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
from curio import Event
class Future:
def __init__(self):
self._event = Event()
self._result = None
self._exception = None
async def set_result(self, result):
self._result = result
await self._event.set()
async def set_exception(self, exception):
self._exception = exception
await self._event.set()
async def _get_result(self):
await self._event.wait()
if self._exception is not None:
raise self._exception
return self._result
def __await__(self):
"""Future is awaitable
PS: I don't know how to implement __await__, but I know coroutine
implemented it, so just forward the call!
"""
return self._get_result().__await__()