uasyncio.core: Add cancel(coro) function.

This also adds CancelledError exception and makes TimeoutError be a
subclass of it. As well as adds default exception handler for it in
the eventloop (which just skips re-adding this coro to the scheduling
queue, as expected).
pull/246/merge
Paul Sokolovsky 2018-01-07 10:41:51 +02:00
rodzic c59c5c6ef8
commit 17a432c1a3
1 zmienionych plików z 15 dodań i 1 usunięć

Wyświetl plik

@ -15,7 +15,11 @@ def set_debug(val):
log = logging.getLogger("uasyncio.core")
class TimeoutError(Exception):
class CancelledError(Exception):
pass
class TimeoutError(CancelledError):
pass
@ -136,6 +140,10 @@ class EventLoop:
if __debug__ and DEBUG:
log.debug("Coroutine finished: %s", cb)
continue
except CancelledError as e:
if __debug__ and DEBUG:
log.debug("Coroutine cancelled: %s", cb)
continue
# Currently all syscalls don't return anything, so we don't
# need to feed anything to the next invocation of coroutine.
# If that changes, need to pass that value below.
@ -226,6 +234,12 @@ _stop_iter = StopIteration()
sleep_ms = SleepMs()
def cancel(coro):
prev = coro.pend_throw(CancelledError())
if prev is False:
_event_loop.call_soon(coro)
class TimeoutObj:
def __init__(self, coro):
self.coro = coro