On 28 December 2015 at 06:27, Andrew Svetlov <[email protected]> wrote:
> asyncio has wait_for coroutine for timeouts. > > It works like: > > async def coro(): > # do long running task > await asyncio.sleep(10000) > > await asyncio.wait_for(coro(), 1.5) > > The approach requires splitting code into two functions: one for waiting > and other for performing the task. > > aiohttp has Timeout class ( > https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/helpers.py#L451-L488 > ). > The code from example may be rewritten as: > > with Timeout(1.5): > # do long running task > await asyncio.sleep(10000) > > In my last email I forgot to comment on your original text: > It raises asyncio.TimeoutError and cancels inner code if timeout runs out. > This statement is wrong. It doesn't cancel just the "inner code". If by "inner code" you mean the code inside the context manager block, then no, it cancels that code and any code that may come after. -- Gustavo J. A. M. Carneiro Gambit Research "The universe is always one step beyond logic." -- Frank Herbert
