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

Ben Darnell ben at bendarnell.com
Sun Jun 5 11:56:30 EDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20160605/ebf0359d/attachment.html>


More information about the Async-sig mailing list