Skip to content

Commit 17a432c

Browse files
committed
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).
1 parent c59c5c6 commit 17a432c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

uasyncio.core/uasyncio/core.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ def set_debug(val):
1515
log = logging.getLogger("uasyncio.core")
1616

1717

18-
class TimeoutError(Exception):
18+
class CancelledError(Exception):
19+
pass
20+
21+
22+
class TimeoutError(CancelledError):
1923
pass
2024

2125

@@ -136,6 +140,10 @@ def run_forever(self):
136140
if __debug__ and DEBUG:
137141
log.debug("Coroutine finished: %s", cb)
138142
continue
143+
except CancelledError as e:
144+
if __debug__ and DEBUG:
145+
log.debug("Coroutine cancelled: %s", cb)
146+
continue
139147
# Currently all syscalls don't return anything, so we don't
140148
# need to feed anything to the next invocation of coroutine.
141149
# If that changes, need to pass that value below.
@@ -226,6 +234,12 @@ def __next__(self):
226234
sleep_ms = SleepMs()
227235

228236

237+
def cancel(coro):
238+
prev = coro.pend_throw(CancelledError())
239+
if prev is False:
240+
_event_loop.call_soon(coro)
241+
242+
229243
class TimeoutObj:
230244
def __init__(self, coro):
231245
self.coro = coro

0 commit comments

Comments
 (0)