<p dir="ltr">What's the use case for the async generator version? Could the yield be replaced by 'await loop.shutting_down()'?</p>
<div class="gmail_extra"><br><div class="gmail_quote">On Nov 16, 2016 10:12 AM, "Yury Selivanov" <<a href="mailto:yselivanov@gmail.com">yselivanov@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">One of the remaining problems with the event loop in asyncio is bootstrapping/finalizing asyncio programs.<br>
<br>
Currently, there are two different scenarios:<br>
<br>
[1] Running a coroutine:<br>
<br>
async def main():<br>
# your program<br>
loop = asyncio.get_event_loop()<br>
try:<br>
loop.run_until_complete(main()<wbr>)<br>
finally:<br>
loop.close()<br>
<br>
[2] Running a server:<br>
<br>
loop = asyncio.get_event_loop()<br>
srv = loop.run_until_complete(<br>
loop.create_server(…))<br>
try:<br>
loop.run_forever()<br>
finally:<br>
try:<br>
srv.close()<br>
loop.run_until_complete(srv.<wbr>wait_closed())<br>
finally:<br>
loop.close()<br>
<br>
Both cases are *incomplete*: they don’t do correct finalization of asynchronous generators. To do that we’ll need to add another 1-3 lines of code (extra try-finally).<br>
<br>
This manual approach is really painful:<br>
<br>
* It makes bootstrapping asyncio code unnecessarily hard.<br>
<br>
* It makes the documentation hard to follow. And we can’t restructure the docs to cover the loop only in the advanced section.<br>
<br>
* Most of the people will never know about `loop.shutdown_asyncgen` coroutine.<br>
<br>
* We don’t have a place to add debug code to let people know that their asyncio program didn’t clean all resources properly (a lot of unordered warnings will be spit out instead).<br>
<br>
In <a href="https://github.com/python/asyncio/pull/465" rel="noreferrer" target="_blank">https://github.com/python/<wbr>asyncio/pull/465</a> I propose to add a new function to asyncio in Python 3.6: asyncio.run().<br>
<br>
The function can either accept a coroutine, which solves [1]:<br>
<br>
async def main():<br>
# your program<br>
asyncio.run(main())<br>
<br>
Or it can accept an asynchronous generator, which solves [2]:<br>
<br>
async def main():<br>
srv = await loop.create_server(…))<br>
try:<br>
yield # let the loop run forever<br>
finally:<br>
srv.close()<br>
await srv.wait_closed()<br>
<br>
asyncio.run(main())<br>
<br>
asyncio.run() solves the following:<br>
<br>
* An easy way to start an asyncio program that properly takes care of loop instantiation and finalization.<br>
<br>
* It looks much better in the docs. With asyncio.run people don’t need to care about the loop at all, most probably will never use it.<br>
<br>
* Easier to experiment with asyncio in REPL.<br>
<br>
* The loop and asynchronous generators will be cleaned up properly.<br>
<br>
* We can add robust debug output to the function, listing the unclosed tasks, servers, connections, asynchronous generators etc, helping people with the cleanup logic.<br>
<br>
* Later, if we need to add more cleanup code to asyncio, we will have a function to add the logic to.<br>
<br>
I feel that we should add this to asyncio. One of the arguments against that, is that overloading asyncio.run to accept both coroutines and asynchronous generators makes the API more complex. If that’s really the case, we can add two functions: asyncio.run(coro) and asyncio.run_forever(async_<wbr>generator).<br>
<br>
Also take a look at <a href="https://github.com/python/asyncio/pull/465" rel="noreferrer" target="_blank">https://github.com/python/<wbr>asyncio/pull/465</a>.<br>
<br>
Thanks,<br>
Yury<br>
______________________________<wbr>_________________<br>
Async-sig mailing list<br>
<a href="mailto:Async-sig@python.org">Async-sig@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/async-sig" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/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/<wbr>codeofconduct/</a></blockquote></div></div>