我正在根据http://www.dabeaz.com/coroutines/Coroutines.pdf尝试协同路由管道
问题是,如何从sink
中获取值,而不是只打印它?
以这个代码为例
def coroutine(func):
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start
@coroutine
def produce(target):
while True:
n = (yield)
target.send(n*10)
@coroutine
def sink():
try:
while True:
n = (yield)
print(n)
except GeneratorExit:
pass
sk = sink()
pipe = produce(sink())
有了这个密码我得到:
>>> pipe.send(10)
100
然后我想得到返回值,而不是打印它,我试图从sink屈服:
@coroutine
def sink():
try:
while True:
yield (yield)
except GeneratorExit:
pass
但它似乎不起作用,pipe.send(10)
仍然返回None
,而不是一个生成器。
那么,我该如何获得返回值呢?
目前没有回答
相关问题 更多 >
编程相关推荐