Compare commits
1 Commits
a69bc00593
...
71cf9e7bd3
Author | SHA1 | Date |
---|---|---|
|
71cf9e7bd3 |
|
@ -579,12 +579,14 @@ def run_trio_task_in_future(
|
||||||
cancel_scope = trio.CancelScope()
|
cancel_scope = trio.CancelScope()
|
||||||
finished: bool = False
|
finished: bool = False
|
||||||
|
|
||||||
# Monkeypatch the returned future's cancel() method to forward
|
# monkey-patch the future's `.cancel()` meth to
|
||||||
# cancellation to the Trio task
|
# allow cancellation relay to `trio`-task.
|
||||||
cancel_message = None
|
cancel_message: str|None = None
|
||||||
orig_cancel = result_future.cancel
|
orig_cancel = result_future.cancel
|
||||||
|
|
||||||
def wrapped_cancel(msg=None):
|
def wrapped_cancel(
|
||||||
|
msg: str|None = None,
|
||||||
|
):
|
||||||
nonlocal cancel_message
|
nonlocal cancel_message
|
||||||
if finished:
|
if finished:
|
||||||
# We're being called back after the task completed
|
# We're being called back after the task completed
|
||||||
|
@ -605,7 +607,6 @@ def run_trio_task_in_future(
|
||||||
return True
|
return True
|
||||||
|
|
||||||
result_future.cancel = wrapped_cancel
|
result_future.cancel = wrapped_cancel
|
||||||
# End of monkeypatching
|
|
||||||
|
|
||||||
async def trio_task() -> None:
|
async def trio_task() -> None:
|
||||||
nonlocal finished
|
nonlocal finished
|
||||||
|
@ -625,8 +626,8 @@ def run_trio_task_in_future(
|
||||||
result_future.set_result(result)
|
result_future.set_result(result)
|
||||||
|
|
||||||
except BaseException as exc:
|
except BaseException as exc:
|
||||||
# The result future gets all the non-Cancelled
|
# the result future gets all the non-Cancelled
|
||||||
# exceptions. Any Cancelled need to keep propagating
|
# exceptions. Any Cancelled need to keep propagating
|
||||||
# out of this stack frame in order to reach the cancel
|
# out of this stack frame in order to reach the cancel
|
||||||
# scope for which they're intended.
|
# scope for which they're intended.
|
||||||
cancelled: BaseException|None
|
cancelled: BaseException|None
|
||||||
|
|
Loading…
Reference in New Issue