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:
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.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?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.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.0async_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With