[Async-sig] async generator confusion or bug?

Yury Selivanov yselivanov at gmail.com
Mon Jun 26 13:48:37 EDT 2017


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



More information about the Async-sig mailing list