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


Groups > comp.lang.python > #94290

Re: Send data to asyncio coroutine

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.034
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; '21,': 0.07; 'emulate': 0.07; 'similar,': 0.09; 'throws': 0.09; 'def': 0.13; 'data)': 0.16; 'different,': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'paused': 0.16; 'wrote:': 0.16; "shouldn't": 0.18; '2015': 0.20; 'trying': 0.22; 'am,': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'data,': 0.27; 'yield': 0.27; 'actual': 0.28; "i'm": 0.30; 'code': 0.30; 'similar': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'quite': 0.35; 'something': 0.35; "wasn't": 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'why': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'hello,': 0.40; 'future': 0.60; 'your': 0.60; "you'll": 0.61; 'future.': 0.67; 'jul': 0.72; '"yield': 0.84; 'from"': 0.84; 'run?': 0.84; 'subject:Send': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=T3BOu6DtCJhG3RUmv9Jm1KdwIkMLF2vweIDRFverDEk=; b=FNGsKcOfYkSwd3BQLaI4NLQRhHWDyz86lXwWABlpPPSNfQ+zhP1qWG4jjBE+HSZYKi RgrHafiD91DOM8++rQCwGNGCFd4WE5RIf9T+gXWxPSw62ghrXlDjbVvAIqTJ/IGyn0jC xmUj8yM1patv1ga+canOyVTAkb+PJgpR1J+dvzrAD0S4bAUcTYtiQ/N63NFUNGCrfhUo bg8Fz4u+IrYpyNd/fTjaaQLP3M0FpjEEiqfGwXKfsfz/SnKHjuXvIZ4tU42/uk7JW0ro Uzgx2uNNzy5orvWn6lCKwcv+v2iHbVo/nvHipMVvG/z4nhRgmyFmnrZxjSEStlbfjrl+ iHNw==
X-Received by 10.107.41.11 with SMTP id p11mr41724820iop.148.1437485773237; Tue, 21 Jul 2015 06:36:13 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <97b62bfd-8b6d-45f0-8597-7799ba0ea4af@googlegroups.com>
References <97b62bfd-8b6d-45f0-8597-7799ba0ea4af@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 21 Jul 2015 07:35:33 -0600
Subject Re: Send data to asyncio coroutine
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.815.1437486150.3674.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437486150 news.xs4all.nl 2875 [2001:888:2000:d::a6]:33874
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94290

Show key headers only | View raw


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

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