Chain two futures so that when one completes, so does the other. The result (or exception) of source will be copied to destination. If destination is cancelled, source gets cancelled too. Compatible with both asyncio.Future and concurrent.futures.Future.
(source, destination)
| 370 | dest.set_result(result) |
| 371 | |
| 372 | def _chain_future(source, destination): |
| 373 | """Chain two futures so that when one completes, so does the other. |
| 374 | |
| 375 | The result (or exception) of source will be copied to destination. |
| 376 | If destination is cancelled, source gets cancelled too. |
| 377 | Compatible with both asyncio.Future and concurrent.futures.Future. |
| 378 | """ |
| 379 | if not isfuture(source) and not isinstance(source, |
| 380 | concurrent.futures.Future): |
| 381 | raise TypeError('A future is required for source argument') |
| 382 | if not isfuture(destination) and not isinstance(destination, |
| 383 | concurrent.futures.Future): |
| 384 | raise TypeError('A future is required for destination argument') |
| 385 | source_loop = _get_loop(source) if isfuture(source) else None |
| 386 | dest_loop = _get_loop(destination) if isfuture(destination) else None |
| 387 | |
| 388 | def _set_state(future, other): |
| 389 | if isfuture(future): |
| 390 | _copy_future_state(other, future) |
| 391 | else: |
| 392 | _set_concurrent_future_state(future, other) |
| 393 | |
| 394 | def _call_check_cancel(destination): |
| 395 | if destination.cancelled(): |
| 396 | if source_loop is None or source_loop is events._get_running_loop(): |
| 397 | source.cancel() |
| 398 | else: |
| 399 | source_loop.call_soon_threadsafe(source.cancel) |
| 400 | |
| 401 | def _call_set_state(source): |
| 402 | if (destination.cancelled() and |
| 403 | dest_loop is not None and dest_loop.is_closed()): |
| 404 | return |
| 405 | if dest_loop is None or dest_loop is events._get_running_loop(): |
| 406 | _set_state(destination, source) |
| 407 | else: |
| 408 | if dest_loop.is_closed(): |
| 409 | return |
| 410 | dest_loop.call_soon_threadsafe(_set_state, destination, source) |
| 411 | |
| 412 | destination.add_done_callback(_call_check_cancel) |
| 413 | source.add_done_callback(_call_set_state) |
| 414 | |
| 415 | |
| 416 | def wrap_future(future, *, loop=None): |
no test coverage detected
searching dependent graphs…