Send data to asyncio coroutine

Ian Kelly ian.g.kelly at gmail.com
Tue Jul 21 15:35:33 CEST 2015


On Tue, Jul 21, 2015 at 5:31 AM,  <jcarmena at gmail.com> wrote:
> Hello, I'm trying to understand and link asyncio with ordinary coroutines. Now I just want to understand how to do this on asyncio:
>
>
> def foo():
>     data = yield 8
>     print(data)
>     yield "bye"
>
> def bar():
>     f = foo()
>     n = f.next()
>     print(n)
>     message = f.send("hello")
>     print(message)
>
>
> What is the equivalent for coro.send("some data") in asyncio?

I don't know of any reason why you couldn't do it just like the above.
However, the exchange would not be asynchronous, if that is your goal.

> coro.send on an asyncio coroutine throws AssertionError: yield from wasn't used with future.

So somehow a future got involved where it shouldn't have been. What
was the actual code that you tried to run?

Note that while "yield" and "yield from" look similar, they are quite
different, and you cannot send to a generator that is currently paused
at a "yield from".

If you want to emulate bidirectional communication similar to
coro.send asynchronously, I think you'll need to use Futures to
mediate, something like this (lightly tested):

@asyncio.coroutine
def foo(fut):
    data, fut = yield from send_to_future(8, fut)
    print("foo", data)
    fut.set_result("bye")

@asyncio.coroutine
def bar():
    n, fut = yield from start_coro(foo)
    print("bar", n)
    message = yield from send_to_future("hello", fut)
    print("bar", message)

def start_coro(coro):
    future = asyncio.Future()
    asyncio.async(coro(future))
    return future

def send_to_future(data, future):
    new_future = asyncio.Future()
    future.set_result((data, new_future))
    return new_future


More information about the Python-list mailing list