12

I have two problems with concurrent.futures:

How to break time.sleep() in a python concurrent.futures?

Conclusion: time.sleep() cannot be interrupted. One solution is: You can write a loop around it and do short sleeps.

See How to break time.sleep() in a python concurrent.futures

Individual timeouts for concurrent.futures?

Conclusion: individual timeouts need to implemented by the user. For example: for each timeout you can call to wait().

See Individual timeouts for concurrent.futures

Question

Does asyncio solve theses problems?

1
  • For the sake of self-containment, can you summarise those other two questions here?
    – deceze
    Jul 29, 2016 at 7:24

2 Answers 2

13
+50

In the asyncio model, execution is scheduled and coordinated by an event loop. To cancel execution of a currently suspended task, you essentially simply have to not resume it. While this works a little different in practice, it should be obvious that this makes cancelling a suspended task simple in theory.

Individual timeouts are certainly possible the same way: whenever you suspend a coroutine to wait for a result, you want to supply a timeout value. The event loop will ensure to cancel the waiting task when that timeout is reached and the task hasn't completed yet.

Some concrete samples:

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> task = asyncio.ensure_future(asyncio.sleep(5))
>>> task.cancel()
>>> loop.run_until_complete(task)
Traceback (most recent call last):
   ...
concurrent.futures._base.CancelledError

In practice, this might be implemented using something like this:

class Foo:
    task = None

    async def sleeper(self):
        self.task = asyncio.sleep(60)
        try:
            await self.task
        except concurrent.futures.CancelledError:
            raise NotImplementedError

While this method is asleep, somebody else can call foo.task.cancel() to wake up the coroutine and let it handle the cancellation. Alternatively whoever calls sleeper() can cancel it directly without giving it a chance to clean up.

Setting timeouts is similarly easy:

>>> loop.run_until_complete(asyncio.wait_for(asyncio.sleep(60), 5))
[ ... 5 seconds later ... ]
Traceback (most recent call last):
   ...
concurrent.futures._base.TimeoutError

Particularly in the context of HTTP request timeouts, see aiohttp:

async def fetch_page(session, url):
    with aiohttp.Timeout(10):
        async with session.get(url) as response:
            assert response.status == 200
            return await response.read()

with aiohttp.ClientSession(loop=loop) as session:
    content = loop.run_until_complete(fetch_page(session, 'http://python.org'))

Obviously each call to fetch_page can decide on its own aiohttp.Timeout value, and each individual instance will throw its own exception when that timeout is reached.

3
  • Wow, great answer. Thank you. Imagine I use the subprocess module (I read that it is supported), then I want to terminate the subprocess somehow. Do you have a hint how to do this?
    – guettli
    Jul 29, 2016 at 11:08
  • Not really, no. You should open a new question for that.
    – deceze
    Jul 29, 2016 at 12:57
  • 3
    @guettli Have a look at the asyncio.subprocess module and those examples.
    – Vincent
    Aug 1, 2016 at 11:41
3

You can raise immediately in its exception (with asyncio.CancelledError).

I use this method for the catch to overcome it:

import asyncio

async def worker():
    try:
        # await for some coroutine process
    except asyncio.CancelledError:
        # Do stuff
        raise asyncio.CancelledError()
    except Exception as exc:
        # Do stuff
        print(exc)
    finally:
        await asyncio.sleep(2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.