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


Groups > comp.lang.python > #73268 > unrolled thread

Re: Question about asyncio

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2014-06-13 09:55 -0600
Last post2014-06-13 09:55 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Question about asyncio Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-13 09:55 -0600

#73268 — Re: Question about asyncio

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-06-13 09:55 -0600
SubjectRe: Question about asyncio
Message-ID<mailman.11058.1402674972.18130.python-list@python.org>
On Fri, Jun 13, 2014 at 5:42 AM, Frank Millman <frank@chagford.com> wrote:
> Now I want to use the functionality of asyncio by using a 'yield from' to
> suspend the currently executing function at a particular point while it
> waits for some information. I find that adding 'yield from' turns the
> function into a generator, which means that the caller has to iterate over
> it.

Hold up; you shouldn't be iterating over the coroutines yourself.
Your choices for invoking an asyncio coroutine are either: 1) schedule
it (the simplest way to do this is by calling asyncio.async); or 2)
using 'yield from' in another coroutine that is already being run as a
task.

> I can avoid that by telling the caller to 'yield from' the generator,
> but then *its* caller has to be modified. Now I find I am going through my
> entire application and changing every function into a coroutine by
> decorating it with @asyncio.coroutine, and changing a simple function call
> to a 'yield from'.

If the caller needs to wait on the result, then I don't think you have
another option but to make it a coroutine also.  However if it doesn't
need to wait on the result, then you can just schedule it and move on,
and the caller doesn't need to be a coroutine itself.  Just be aware
that this could result in different behavior from the threaded
approach, since whatever the function does after the scheduling will
happen before the coroutine is started rather than after.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web