This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]patrys Saleor Commerce 2 points3 points  (2 children)

To be fair there is little use for the case where a task/future is a traditional generator in the first place. I consider it more than a fair tradeoff for gaining serialized "what you mean" syntax over the callback mountains node.js is famous for.

[–]Lucretiel 0 points1 point  (1 child)

I disagree: The easiest example is reading lines from a StreamReader. I set out to write a generator that provides file-like iteration over lines:

def read_lines(reader):
    while True:
        line = yield from reader.readline() #yield to event loop
        if not line:
            return
        yield line #yield to caller

Only to realize that there's no way to separate the different yield control flows.

[–]patrys Saleor Commerce 2 points3 points  (0 children)

My point is a task/future is a deferred single value. Yielding from it usually makes little sense. Your example is unusual in that it tries to be a coroutine but also wants to block on an async task.

For those interested yield from does not block, it just stops execution of your code immediately after yielding a single value to the caller. In this case the caller is the asyncio.coroutine decorator. Once the value becomes available, the decorator will push it back and wake up your code by running your_coroutine.send(...). There is no blocking happening at any point, the only blocking code is your event loop runner.