1

I thought I had grokked coroutines with David Beazley's very good presentation but I can't reconcile it fully with the new syntax described in PEP-492.

In the presentation, he explains how coroutines can be thought of as a pipeline that gets pushed to as opposed to pulled from like in generators.

For example:

# 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())

How can one implement the printer() coroutine using this new syntax? I have not yet seen an example where the coroutine gets pushed to using this new syntax. Is it possible?

2
  • This doesn't really have a whole lot to do with asyncio. If it doesn't use an event loop, it's not really asynchronous in the sense of what the asyncio module provides. Commented Jul 21, 2016 at 11:38
  • 3
    This example is about traditional coroutines, where coroutines decide which other coroutine should run, and what value should be sent to it. This is hard to maintain and usually is not necessary, so async coroutines behave differently. All they can do is to give up their running right and pass an awaitable future object which represents the task of which the continuation should be this coroutine. And there is an event loop which arranges all this, it runs and sends the right values to coroutines when awaitables complete. Commented Jul 21, 2016 at 11:44

1 Answer 1

1

What you have there is not a coroutine in the sense of the asyncio module and/or PEP-492. As the PEP itself says:

[This PEP] is relevant only to the kind of coroutine that uses yield as a signal to the scheduler, indicating that the coroutine will be waiting until an event (such as IO) is completed.

  1. There's no scheduler (event loop) involved in your example, and
  2. the coroutine is not using yield only "as a signal to the scheduler"; it is really using it to read data.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.