首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Asyncio协同素

Asyncio协同素
EN

Stack Overflow用户
提问于 2016-07-21 11:20:10
回答 1查看 314关注 0票数 1

我以为我已经与David的非常好的介绍性进行了协作,但我无法将它与佩普-492中描述的新语法完全兼容。

在演示中,他解释了如何将协同线看作是一条管道,而不是从发电机中拔出的管道。

例如:

代码语言:javascript
复制
# 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被推到使用这个新语法。有可能吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-21 11:46:36

您所拥有的并不是asyncio模块和/或PEP-492意义上的协同线。正如PEP本身所说:

此PEP仅与使用yield作为调度程序信号的协同器相关,指示协同线将等待事件(例如IO)完成。

  1. 在您的示例中不涉及调度程序(事件循环),而且
  2. coroutine并不仅仅使用yield“作为调度程序的信号”;它实际上是用来读取数据的。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38502885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档