【发布时间】:2015-01-27 05:10:27
【问题描述】:
这是follow-up co-routine question to another question。这段代码sn-p也摘自david beazley的示例代码(inline1.py)
为什么Got: None 没有打印出来?
class Task:
def __init__(self, gen):
self._gen = gen
def step(self, value=None):
# Run to the next yield
try:
fut = self._gen.send(value)
# Future returned
fut.add_done_callback(self._wakeup)
except StopIteration as exc:
pass
def _wakeup(self, fut):
# Handler of results
result = fut.result()
#print(result)
self.step(result) # Feedback loop(run to next yield)
if __name__ == '__main__':
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(max_workers=8)
def func(x, y):
time.sleep(1)
return x + y
def do_func(x, y):
result = yield pool.submit(func, x, y)
print('Got:', result)
t = Task(do_func(2,3))
t.step()
【问题讨论】:
-
这与 SSCCE 几乎相反。
-
@CharlesDuffy 感谢 cmets。你介意分享你对哪里改进的建议吗?谢谢。
-
sscce.org 将是开始的地方 - 或 stackoverflow.com/help/mcve,本地等效项。