<div dir="ltr">Regarding to asyncio event loop globalness.<br><br>In my code I never use implicit global event loop but always pass the instance everywhere when needed.<br><br>aiohttp as well as other aio-libs libraries supports optional loop=None parameter.<br>The only reason why the loop is optional -- we are trying to follow asyncio conventions.<br>But frankly speaking I found implicit event loop both non-obvious and error-prone.<br><br>It would be nice to always pass event loop explicitly in asyncio-based code but ship has sailed years ago.</div><br><div class="gmail_quote"><div dir="ltr">On Sun, Jun 5, 2016 at 11:38 AM Nathaniel Smith <<a href="mailto:njs@pobox.com">njs@pobox.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Sun, Jun 5, 2016 at 10:24 AM, Ben Darnell <<a href="mailto:ben@bendarnell.com" target="_blank">ben@bendarnell.com</a>> wrote:<br>
[...]<br>
> No, it's not helpful because event loops and coroutine runners are<br>
> independent. It's easy to find the current event loop (there are globals for<br>
> that), but you can't really interact with the coroutine runner except via<br>
> yield/await (unless we introduce new globals for this purpose)<br>
<br>
Of course the reason it takes the loop argument is that in asyncio,<br>
event loops and coroutine runners aren't independent at all -- the<br>
loop is a global, and then the current coroutine runner is part of the<br>
loop state, so effectively the current coroutine runner is already a<br>
global:<br>
<a href="https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.current_task" rel="noreferrer" target="_blank">https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.current_task</a><br>
<br>
In the curio approach they also aren't independent -- they don't have<br>
any magic globals, but for them the event loop and coroutine runner<br>
are the same object.<br>
<br>
And AFAICT the tornado.gen.with_timeout wrapper doesn't deal with<br>
coroutines/coroutine runners at all? I just read the source, but I<br>
couldn't fully follow it. If I write<br>
async def do_stuff():<br>
try:<br>
await aiohttp.get("<a href="https://python.org" rel="noreferrer" target="_blank">https://python.org</a>")<br>
except:<br>
print("oops, cleaning up")<br>
raise<br>
else:<br>
print("did it!")<br>
await tornado.gen.with_timeout(timedelta(seconds=0.0001), do_stuff())<br>
and the timeout fires, then is the output "oops, cleaning up", "did<br>
it!", or nothing at all?<br>
<br>
I guess the main case where coroutine runners are really independent<br>
from an event loop is when you have random little ad hoc coroutine<br>
runners that aren't directly integrated with an event loop at all.<br>
E.g., in my async_generator package the @async_generator decorator<br>
acts as a coroutine runner that adapts a regular coroutine into an<br>
async iterator, by intercepting special 'await yield_(...)' messages<br>
and transparently proxying other messages to the underlying coroutine<br>
runner. Before I read the source I was expecting that maybe<br>
tornado.gen.timeout_after would work in a broadly similar way, but<br>
apparently I was wrong :-).<br>
<br>
I don't have any argument here -- just trying to think aloud to<br>
understand the design space better...<br>
<br>
-n<br>
<br>
--<br>
Nathaniel J. Smith -- <a href="https://vorpus.org" rel="noreferrer" target="_blank">https://vorpus.org</a><br>
_______________________________________________<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/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/codeofconduct/</a><br>
</blockquote></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><div>Thanks,</div>Andrew Svetlov</div></div>