-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask2.py
More file actions
43 lines (30 loc) · 818 Bytes
/
task2.py
File metadata and controls
43 lines (30 loc) · 818 Bytes
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
# coding: utf-8
# In[1]:
import future
from future import Future
# In[2]:
class Task(Future):
"""
Wraps a generator yielding a Future
object and abstracts away the handling of future API
"""
def __init__(self, gen):
super().__init__()
self.gen = gen
def step(self, snd_val=None, exp=None):
""""""
try:
if exp:
self.gen.throw(exp)
else:
fut = self.gen.send(snd_val)
fut.add_done_callback(self._fut_done_cb)
except StopIteration as e:
self.set_result(snd_val)
def _fut_done_cb(self, fut):
try:
result = fut.result()
self.step(result, None)
except Exception as e:
self.step(None, e)
pass