lovetl2002
lovetl2002

Reputation: 1068

async_timeout.timeout vs asyncio.wait_for

Previously I used asyncio.wait_for for timeout control and it worked pretty well. Recently, I learned aiohttp package and found that it used asyncio_timeout.timeout for timeout control instead. Then I read the github page (https://github.com/aio-libs/async-timeout) of asyncio_timeout. The author claimed that it runs faster than asyncio.wait_for. So I have two questions:

  1. Can asyncio_timeout.timeout completely replace asyncio.wait_for? Should I replace all asyncio.wait_for to increase the speed? I'm writing a websocket client and asyncio.wait_for currently controls websocket.recv which is called frequently.
  2. In the "Usage example" section (https://github.com/aio-libs/async-timeout), it seems that asyncio_timeout.timeout should be used with async with. But in the aiohttp help page, they use with instead of async with (http://aiohttp.readthedocs.io/en/stable/). So which one is correct?

Upvotes: 1

Views: 5690

Answers (1)

Andrew Svetlov
Andrew Svetlov

Reputation: 17376

  1. asyncio_timeout.timeout is faster than asyncio.wait_for, true. wait_for creates a new task. It maybe doesn't matter for application code but very sufficient for libraries. For example asyncpg tried to use wait_for but rejected for sake of speed.
  2. asyncio_timeout could be used everywhere except tornado.web.RequestHandler.get etc. Tornado still doesn't support tasks cancellation, I hope it will be fixed in tornado 5.0
  3. Technically async_timeout.timeout works with both async with and just with. People were confused by with statement many times: in asyncio world async operations are encouraged. That's why I added asynchronous context manager support and encourage this usage. with will be supported for a long time for sake of backward compatibility anyway -- I just don't want to encourage this syntax.

UPD Python 3.11+ has native async with asyncio.timeout(): support which is modelled after async_timeout but behaves more correctly in corner cases.

Upvotes: 9

Related Questions