Question about asyncio

Ian Kelly ian.g.kelly at gmail.com
Fri Jun 13 11:55:23 EDT 2014


On Fri, Jun 13, 2014 at 5:42 AM, Frank Millman <frank at 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.



More information about the Python-list mailing list