|
36 | 36 | threadlocals, |
37 | 37 | ) |
38 | 38 | from ._core._eventloop import run as run_eventloop |
39 | | -from ._core._exceptions import NoEventLoopError |
| 39 | +from ._core._exceptions import NoEventLoopError, RunFinishedError |
40 | 40 | from ._core._synchronization import Event |
41 | 41 | from ._core._tasks import CancelScope, create_task_group |
42 | 42 | from .abc._tasks import TaskStatus |
@@ -253,9 +253,16 @@ def callback(f: Future[T_Retval]) -> None: |
253 | 253 | if event_loop_thread_id == get_ident(): |
254 | 254 | scope.cancel("the future was cancelled") |
255 | 255 | elif event_loop_thread_id is not None: |
256 | | - run_sync( |
257 | | - scope.cancel, "the future was cancelled", token=self._token |
258 | | - ) |
| 256 | + try: |
| 257 | + self._token.backend_class.run_sync_soon( |
| 258 | + scope.cancel, |
| 259 | + ("the future was cancelled",), |
| 260 | + self._token.native_token, |
| 261 | + ) |
| 262 | + except RunFinishedError: |
| 263 | + # The target event loop is gone, so there's nothing left to |
| 264 | + # cancel or notify |
| 265 | + pass |
259 | 266 |
|
260 | 267 | try: |
261 | 268 | retval_or_awaitable = func(*args, **kwargs) |
@@ -339,6 +346,100 @@ def call( |
339 | 346 | """ |
340 | 347 | return cast(T_Retval, self.start_task_soon(func, *args).result()) |
341 | 348 |
|
| 349 | + @overload |
| 350 | + async def call_async( |
| 351 | + self, |
| 352 | + func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]], |
| 353 | + *args: Unpack[PosArgsT], |
| 354 | + abandon_on_cancel: bool = False, |
| 355 | + ) -> T_Retval: ... |
| 356 | + |
| 357 | + @overload |
| 358 | + async def call_async( |
| 359 | + self, |
| 360 | + func: Callable[[Unpack[PosArgsT]], T_Retval], |
| 361 | + *args: Unpack[PosArgsT], |
| 362 | + abandon_on_cancel: bool = False, |
| 363 | + ) -> T_Retval: ... |
| 364 | + |
| 365 | + async def call_async( |
| 366 | + self, |
| 367 | + func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval] | T_Retval], |
| 368 | + *args: Unpack[PosArgsT], |
| 369 | + abandon_on_cancel: bool = False, |
| 370 | + ) -> T_Retval: |
| 371 | + """ |
| 372 | + Call the given function in the portal's event loop from another event loop. |
| 373 | +
|
| 374 | + If the callable returns an awaitable, it is awaited on. Cancelling the calling |
| 375 | + task cancels the task running in the portal. |
| 376 | +
|
| 377 | + :param func: any callable |
| 378 | + :param abandon_on_cancel: if ``False``, wait for the cancelled task to finish |
| 379 | + and propagate any exception raised during cleanup, or otherwise re-raise |
| 380 | + the caller's cancellation; if ``True``, stop waiting immediately and ignore |
| 381 | + the task's subsequent return value or exception |
| 382 | + :raises RuntimeError: if the portal is not running or if this method is called |
| 383 | + from within the portal's event loop thread |
| 384 | +
|
| 385 | + .. versionadded:: 4.16.0 |
| 386 | + """ |
| 387 | + self._check_running() |
| 388 | + source_token = current_token() |
| 389 | + event = Event() |
| 390 | + future: Future[T_Retval] = Future() |
| 391 | + cancellation: Future[None] = Future() |
| 392 | + |
| 393 | + async def run_call() -> None: |
| 394 | + with CancelScope() as scope: |
| 395 | + |
| 396 | + def cancel_target(_: Future[None]) -> None: |
| 397 | + try: |
| 398 | + self._token.backend_class.run_sync_soon( |
| 399 | + scope.cancel, |
| 400 | + ("the calling task was cancelled",), |
| 401 | + self._token.native_token, |
| 402 | + ) |
| 403 | + except RunFinishedError: |
| 404 | + pass |
| 405 | + |
| 406 | + cancellation.add_done_callback(cancel_target) |
| 407 | + await self._call_func(func, args, {}, future) |
| 408 | + |
| 409 | + def notify_source(_: Future[T_Retval]) -> None: |
| 410 | + try: |
| 411 | + source_token.backend_class.run_sync_soon( |
| 412 | + event.set, (), source_token.native_token |
| 413 | + ) |
| 414 | + except RunFinishedError: |
| 415 | + pass |
| 416 | + |
| 417 | + def spawn_task() -> None: |
| 418 | + try: |
| 419 | + self._task_group.start_soon(run_call) |
| 420 | + except BaseException as exc: |
| 421 | + if not future.done(): |
| 422 | + future.set_exception(exc) |
| 423 | + |
| 424 | + future.add_done_callback(notify_source) |
| 425 | + self._token.backend_class.run_sync_soon( |
| 426 | + spawn_task, (), self._token.native_token |
| 427 | + ) |
| 428 | + try: |
| 429 | + await event.wait() |
| 430 | + except get_cancelled_exc_class(): |
| 431 | + cancellation.cancel() |
| 432 | + if not abandon_on_cancel: |
| 433 | + with CancelScope(shield=True): |
| 434 | + await event.wait() |
| 435 | + |
| 436 | + if not future.cancelled(): |
| 437 | + future.result() |
| 438 | + |
| 439 | + raise |
| 440 | + |
| 441 | + return future.result() |
| 442 | + |
342 | 443 | @overload |
343 | 444 | def start_task_soon( |
344 | 445 | self, |
|
0 commit comments