我以为我已经与David的非常好的介绍性进行了协作,但我无法将它与佩普-492中描述的新语法完全兼容。
在演示中,他解释了如何将协同线看作是一条管道,而不是从发电机中拔出的管道。
例如:
# cofollow.py
#
# A simple example showing how to hook up a pipeline with
# coroutines. To run this, you will need a log file.
# Run the program logsim.py in the background to get a data
# source.
from coroutine import coroutine
# A data source. This is not a coroutine, but it sends
# data into one (target)
import time
def follow(thefile, target):
thefile.seek(0,2) # Go to the end of the file
while True:
line = thefile.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
target.send(line)
# A sink. A coroutine that receives data
@coroutine
def printer():
while True:
line = (yield)
print line,
# Example use
if __name__ == '__main__':
f = open("access-log")
follow(f,printer())如何使用这种新的语法实现printer()协同?我还没有见过这样一个例子,在这个例子中,coroutine被推到使用这个新语法。有可能吗?
发布于 2016-07-21 11:46:36
您所拥有的并不是asyncio模块和/或PEP-492意义上的协同线。正如PEP本身所说:
此PEP仅与使用
yield作为调度程序信号的协同器相关,指示协同线将等待事件(例如IO)完成。
yield“作为调度程序的信号”;它实际上是用来读取数据的。https://stackoverflow.com/questions/38502885
复制相似问题