Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94706

Re: Send data to asyncio coroutine

Newsgroups comp.lang.python
Date 2015-07-28 14:17 -0700
References <97b62bfd-8b6d-45f0-8597-7799ba0ea4af@googlegroups.com> <mailman.815.1437486150.3674.python-list@python.org>
Message-ID <1195c0a3-05b5-4213-92a7-db005ad7d547@googlegroups.com> (permalink)
Subject Re: Send data to asyncio coroutine
From Javier <jcarmena@gmail.com>

Show all headers | View raw


El martes, 21 de julio de 2015, 15:42:47 (UTC+2), Ian  escribió:
> On Tue, Jul 21, 2015 at 5:31 AM,  <jcarmena@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




Hello again. I have been investigating a bit your example. I don't understand why I can't write something like this:

--------

import asyncio

def foo():
    print("start foo")
    try:
        while True:
            val = yield
            print("foo:", val)
            yield from asyncio.sleep(3)
    except GeneratorExit:
        print("foo closed")
    print("exit foo")

def bar(next):
    print("start bar")
    next.send(None)
    try:
        while True:
            val = yield
            next.send("bar/"+val)
    except GeneratorExit:
        print("bar closed")
    print("exit bar")

def fun(next):
    next.send(None)
    for e in ["hello", "world", "I'm", "pythonist"]:
        next.send(e)

@asyncio.coroutine
def run():
    fun(bar(foo()))

loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()

-------

The expected output is:

start bar
start foo
foo: bar/hello
foo: bar/world
foo: bar/I'm
foo: bar/phytonist
bar closed
exit bar
foo closed
exit foo

But the yield from asyncio.sleep(3) call raises AssertionError, however it's inside a Task!
I think this is a big flaw in python/asyncio design.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Send data to asyncio coroutine jcarmena@gmail.com - 2015-07-21 04:31 -0700
  Re: Send data to asyncio coroutine Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-21 07:35 -0600
    Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-07-28 14:17 -0700
      Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-07-28 15:41 -0700
        Re: Send data to asyncio coroutine Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-28 15:20 -0800
      Re: Send data to asyncio coroutine Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-28 15:06 -0800
        Re: Send data to asyncio coroutine Rustom Mody <rustompmody@gmail.com> - 2015-07-28 19:52 -0700
        Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-07-29 07:24 -0700
  Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-08-01 09:07 -0700
    Re: Send data to asyncio coroutine Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-01 17:41 +0100
      Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-08-01 11:22 -0700
        Re: Send data to asyncio coroutine Marko Rauhamaa <marko@pacujo.net> - 2015-08-01 21:38 +0300
          Re: Send data to asyncio coroutine Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-01 19:45 +0100
            Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-08-01 12:07 -0700
              Re: Send data to asyncio coroutine Marko Rauhamaa <marko@pacujo.net> - 2015-08-01 22:14 +0300
                Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-08-01 17:50 -0700
              Re: Send data to asyncio coroutine Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-01 20:33 +0100
              Re: Send data to asyncio coroutine Chris Angelico <rosuav@gmail.com> - 2015-08-02 09:28 +1000
            Re: Send data to asyncio coroutine Marko Rauhamaa <marko@pacujo.net> - 2015-08-01 22:09 +0300
              Re: Send data to asyncio coroutine Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-01 20:29 +0100
    Re: Send data to asyncio coroutine Marko Rauhamaa <marko@pacujo.net> - 2015-08-01 20:18 +0300
      Re: Send data to asyncio coroutine Javier <jcarmena@gmail.com> - 2015-08-01 10:50 -0700
        Re: Send data to asyncio coroutine Marko Rauhamaa <marko@pacujo.net> - 2015-08-01 21:33 +0300

csiph-web