<div dir="ltr"><div>I'd like to suggest another way around some of the issues here, with apologies if this has already been discussed sometime in the past.</div><div><br></div><div>From the viewpoint of a Python programmer, there are two distinct reasons for wanting to suspend execution in a block of code:</div><div><br></div><div>1. To yield a value from an iterator, as Python generators do today. </div><div><br></div><div>2. To cede control to the event loop while waiting for an asynchronous task to make progress in a coroutine. </div><div><br></div><div>As of today both of these reasons to suspend are supported by the same underlying mechanism, i.e. a "yield" at the end of the chain of "yield from"s. PEPs 492 and 3152 introduce "await" and "cocall", but at the bottom of it all there's effectively still a yield as I understand it. </div><div><br></div><div>I think that the fact that these two concepts use the same mechanism is what leads to the issues with coroutine-generators that Greg and Yury have raised.</div><div><br></div><div>With that in mind, would it be possible to introduce a second form of suspension to Python to specifically handle the case of ceding to the event loop? I don't know what the implementation complexity of this would be, or if it's even feasible. But roughly speaking, the syntax for this could use "await", and code would look just like it does in the PEP. The semantics of "await <Task>" would be analogous to "yield from <Task>" today, with the difference that the Task would go up the chain of "await"s to the outermost caller, which would typically be asyncio, with some modifications from its form today. Progress would be made via __anext__ instead of __next__.</div><div><br></div><div>Again, this might be impossible to do, but the mental model for the Python programmer becomes cleaner, I think. Most of the issues around combining generators and coroutines would go away - you could freely use "await" inside a generator since it cedes control to the event loop, not the caller of the generator. All of the "async def"/"await" examples in PEP 492 would work as is. It might also make it easier in the future to add support for async calls insider __getattr__ etc.</div><div><br></div><div>Thanks for reading!</div><div>Rajiv</div><div><br></div><div><br></div><div class="gmail_extra"><br></div></div>