<div dir="ltr"><div dir="auto">I actually thought that async generators already guarded against this using their ag_running attribute. If I try running Dima's example with async_generator, I get:<br><br>sending user-1<br>received user-1<br>sending user-2<br>sending user-0<br>Traceback (most recent call last):<br>[...]<br>ValueError: async generator already executing<br><br>The relevant code is here:<br><a href="https://github.com/njsmith/async_generator/blob/e303e077c9dcb5880c0ce9930d560b282f8288ec/async_generator/impl.py#L273-L279">https://github.com/njsmith/async_generator/blob/e303e077c9dcb5880c0ce9930d560b282f8288ec/async_generator/impl.py#L273-L279</a><br><br></div><div>But I added this in the first place because I thought it was needed for compatibility with native async generators :-)<br></div><div dir="auto"><br></div><div>-n<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Jun 26, 2017 6:54 PM, "Yury Selivanov" <<a href="mailto:yselivanov@gmail.com" target="_blank">yselivanov@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">(Posting here, rather than to the issue, because I think this actually needs more exposure).<br>
<br>
I looked at the code (genobject.c) and I think I know what's going on here.  Normally, when you work with an asynchronous generator (AG) you interact with it through "asend" or "athrow" *coroutines*.<br>
<br>
Each AG has its own private state, and when you await on "asend" coroutine you are changing that state.  The state changes on each "asend.send" or "asend.throw" call.  The normal relation between AGs and asends is 1 to 1.<br>
<br>
  AG - asend<br>
<br>
However, in your example you change that to 1 to many:<br>
<br>
     asend<br>
    /<br>
  AG - asend<br>
    \<br>
     asend<br>
<br>
Both 'ensure_future' and 'gather' will wrap each asend coroutine into an 'asyncio.Task'. And each Task will call "asend.send(None)" right in its '__init__', which changes the underlying *shared* AG instance completely out of order.<br>
<br>
I don't see how this can be fixed (or that it even needs to be fixed), so I propose to simply raise an exception if an AG has more than one asends changing it state *at the same time*.<br>
<br>
Thoughts?<br>
<br>
Yury<br>
<br>
> On Jun 26, 2017, at 12:25 PM, Dima Tisnek <<a href="mailto:dimaqq@gmail.com" target="_blank">dimaqq@gmail.com</a>> wrote:<br>
><br>
> Hi group,<br>
><br>
> I'm trying to cross-use an sync generator across several async functions.<br>
> Is it allowed or a completely bad idea? (if so, why?)<br>
><br>
> Here's MRE:<br>
><br>
> import asyncio<br>
><br>
><br>
> async def generator():<br>
>    while True:<br>
>        x = yield<br>
>        print("received", x)<br>
>        await asyncio.sleep(0.1)<br>
><br>
><br>
> async def user(name, g):<br>
>    print("sending", name)<br>
>    await g.asend(name)<br>
><br>
><br>
> async def helper():<br>
>    g = generator()<br>
>    await g.asend(None)<br>
><br>
>    await asyncio.gather(*[user(f"user-{<wbr>x}", g) for x in range(3)])<br>
><br>
><br>
> if __name__ == "__main__":<br>
>    asyncio.get_event_loop().run_u<wbr>ntil_complete(helper())<br>
><br>
><br>
> And the output it produces when ran (py3.6.1):<br>
><br>
> sending user-1<br>
> received user-1<br>
> sending user-2<br>
> sending user-0<br>
> received None<br>
> received None<br>
><br>
><br>
> Where are those None's coming from in the end?<br>
> Where did "user-0" and "user-1" data go?<br>
><br>
> Is this a bug, or am I hopelessly confused?<br>
> Thanks!<br>
> ______________________________<wbr>_________________<br>
> Async-sig mailing list<br>
> <a href="mailto:Async-sig@python.org" target="_blank">Async-sig@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/async-sig" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/async-sig</a><br>
> Code of Conduct: <a href="https://www.python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">https://www.python.org/psf/cod<wbr>eofconduct/</a><br>
<br>
</blockquote></div></div>
</div>