[Async-sig] async generator confusion or bug?

Andrew Svetlov andrew.svetlov at gmail.com
Mon Jun 26 13:53:25 EDT 2017


IIRC gather collects coroutines in arbitrary order, maybe it's the source
of misunderstanding?

On Mon, Jun 26, 2017 at 8:48 PM Yury Selivanov <yselivanov at gmail.com> wrote:

> Hi Dima,
>
> > On Jun 26, 2017, at 12:25 PM, Dima Tisnek <dimaqq at gmail.com> wrote:
> >
> > Hi group,
> >
> > I'm trying to cross-use an sync generator across several async functions.
> > Is it allowed or a completely bad idea? (if so, why?)
>
> It is allowed, but leads to complex code.
>
> >
> > Here's MRE:
> >
> > import asyncio
> >
> >
> > async def generator():
> >    while True:
> >        x = yield
> >        print("received", x)
> >        await asyncio.sleep(0.1)
> >
> >
> > async def user(name, g):
> >    print("sending", name)
> >    await g.asend(name)
> >
> >
> > async def helper():
> >    g = generator()
> >    await g.asend(None)
> >
> >    await asyncio.gather(*[user(f"user-{x}", g) for x in range(3)])
> >
> >
> > if __name__ == "__main__":
> >    asyncio.get_event_loop().run_until_complete(helper())
> >
> >
> > And the output it produces when ran (py3.6.1):
> >
> > sending user-1
> > received user-1
> > sending user-2
> > sending user-0
> > received None
> > received None
> >
> >
> > Where are those None's coming from in the end?
> > Where did "user-0" and "user-1" data go?
>
>
> Interesting.  If I replace "gather" with three consecutive awaits of
> "asend", everything works as expected.  So there's some weird interaction
> of asend/gather, or maybe you did find a bug.  Need more time to
> investigate.
>
> Would you mind to open an issue on bugs.python?
>
> Thanks,
> Yury
>
> _______________________________________________
> Async-sig mailing list
> Async-sig at python.org
> https://mail.python.org/mailman/listinfo/async-sig
> Code of Conduct: https://www.python.org/psf/codeofconduct/
>
-- 
Thanks,
Andrew Svetlov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20170626/86140061/attachment-0001.html>


More information about the Async-sig mailing list