[Async-sig] asyncio.timeout() is not portable

Guido van Rossum guido at python.org
Sun Jun 5 13:02:31 EDT 2016


So in the case of that Tornado example, who does the sleeping (or how
is the exception scheduled)? We may not be able to retract this from
3.5.2 (RC1 goes out next weekend) but asyncio is still provisional
until the 3.6 feature freeze in September. What's a better design?
(FWIW asyncio.timeout() takes an optional loop parameter, but that's
probably not helpful.)

On Sun, Jun 5, 2016 at 8:56 AM, Ben Darnell <ben at bendarnell.com> wrote:
> It has come to my attention (https://github.com/KeepSafe/aiohttp/issues/877)
> that Python 3.5.2 (!) introduced a new context manager asyncio.timeout,
> which attaches a timeout to the current asyncio.Task. Any library or
> application that uses this feature will not be portable to other coroutine
> runners.
>
>     with asyncio.timeout(1):
>         result = await aiohttp.get('http://www.example.com')
>
> It's difficult to make this interface portable. We'd need to introduce a
> global thread-specific object that all of the coroutine runners could
> register the current task on, and define an interface that asyncio.timeout
> would call on that object to set the timeout. We could get rid of the global
> if the context manager used `async with` instead of `with`, since that would
> give us a way to communicate with the coroutine runner directly, but the net
> result is still that each runner needs to implement hooks for this feature
> (the hooks may or may not be generalizable to other features in the future).
>
> If our goal is portability across coroutine runners, then asyncio.timeout()
> needs to go back to the drawing board. If the goal is to extend asyncio.Task
> to be the one true coroutine runner then we could leave it as-is.
>
> For comparison, Tornado's equivalent to asyncio.timeout is
> tornado.gen.with_timeout(), and it has no special interactions with the
> coroutine runner:
>
>     result = await with_timeout(timedelta(seconds=1),
>                      convert_yielded(aiohttp.get("http://www.example.com")))
>
> (The convert_yielded call won't be needed in the next release of Tornado.
> The use of timedelta is because Tornado prefers absolute deadlines instead
> of relative timeouts, so that's how plain numbers are interpreted).
>
> -Ben
>
> _______________________________________________
> Async-sig mailing list
> Async-sig at python.org
> https://mail.python.org/mailman/listinfo/async-sig
> Code of Conduct: https://www.python.org/psf/codeofconduct/



-- 
--Guido van Rossum (python.org/~guido)


More information about the Async-sig mailing list