|
| 1 | +import asyncio |
| 2 | +import contextvars |
| 3 | +import functools |
| 4 | +import sys |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import sniffio |
| 8 | +import trio.lowlevel |
| 9 | +import trio.to_thread |
| 10 | + |
| 11 | +from . import _asyncio |
| 12 | +from ._context import restore_context as _restore_context |
| 13 | + |
| 14 | + |
| 15 | +class TrioThreadCancelled(BaseException): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +def get_running_loop(): |
| 20 | + try: |
| 21 | + asynclib = sniffio.current_async_library() |
| 22 | + except sniffio.AsyncLibraryNotFoundError: |
| 23 | + return asyncio.get_running_loop() |
| 24 | + |
| 25 | + if asynclib == "asyncio": |
| 26 | + return asyncio.get_running_loop() |
| 27 | + if asynclib == "trio": |
| 28 | + return trio.lowlevel.current_token() |
| 29 | + raise RuntimeError(f"unsupported library {asynclib}") |
| 30 | + |
| 31 | + |
| 32 | +@trio.lowlevel.disable_ki_protection |
| 33 | +async def wrap_awaitable(awaitable): |
| 34 | + return await awaitable |
| 35 | + |
| 36 | + |
| 37 | +def create_task_threadsafe(loop, awaitable): |
| 38 | + if isinstance(loop, trio.lowlevel.TrioToken): |
| 39 | + try: |
| 40 | + loop.run_sync_soon( |
| 41 | + trio.lowlevel.spawn_system_task, |
| 42 | + wrap_awaitable, |
| 43 | + awaitable, |
| 44 | + ) |
| 45 | + except trio.RunFinishedError: |
| 46 | + raise RuntimeError("trio loop no-longer running") |
| 47 | + |
| 48 | + return _asyncio.create_task_threadsafe(loop, awaitable) |
| 49 | + |
| 50 | + |
| 51 | +async def run_in_executor(*, loop, executor, thread_handler, child): |
| 52 | + if isinstance(loop, trio.lowlevel.TrioToken): |
| 53 | + context = contextvars.copy_context() |
| 54 | + func = context.run |
| 55 | + task_context: list[asyncio.Task[Any]] = [] |
| 56 | + |
| 57 | + # Run the code in the right thread |
| 58 | + full_func = functools.partial( |
| 59 | + thread_handler, |
| 60 | + loop, |
| 61 | + sys.exc_info(), |
| 62 | + task_context, |
| 63 | + func, |
| 64 | + child, |
| 65 | + ) |
| 66 | + try: |
| 67 | + if executor is None: |
| 68 | + |
| 69 | + async def handle_cancel(): |
| 70 | + try: |
| 71 | + await trio.sleep_forever() |
| 72 | + except trio.Cancelled: |
| 73 | + if task_context: |
| 74 | + task_context[0].cancel() |
| 75 | + raise |
| 76 | + |
| 77 | + async with trio.open_nursery() as nursery: |
| 78 | + nursery.start_soon(handle_cancel) |
| 79 | + try: |
| 80 | + return await trio.to_thread.run_sync( |
| 81 | + thread_handler, func, abandon_on_cancel=False |
| 82 | + ) |
| 83 | + except TrioThreadCancelled: |
| 84 | + pass |
| 85 | + finally: |
| 86 | + nursery.cancel_scope.cancel() |
| 87 | + else: |
| 88 | + event = trio.Event() |
| 89 | + |
| 90 | + def callback(fut): |
| 91 | + loop.run_sync_soon(event.set) |
| 92 | + |
| 93 | + fut = executor.submit(full_func) |
| 94 | + fut.add_done_callback(callback) |
| 95 | + |
| 96 | + async def handle_cancel_fut(): |
| 97 | + try: |
| 98 | + await trio.sleep_forever() |
| 99 | + except trio.Cancelled: |
| 100 | + fut.cancel() |
| 101 | + if task_context: |
| 102 | + task_context[0].cancel() |
| 103 | + raise |
| 104 | + |
| 105 | + async with trio.open_nursery() as nursery: |
| 106 | + nursery.start_soon(handle_cancel_fut) |
| 107 | + with trio.CancelScope(shield=True): |
| 108 | + await event.wait() |
| 109 | + nursery.cancel_scope.cancel() |
| 110 | + try: |
| 111 | + return fut.result() |
| 112 | + except TrioThreadCancelled: |
| 113 | + pass |
| 114 | + finally: |
| 115 | + _restore_context(context) |
| 116 | + |
| 117 | + return await _asyncio.run_in_executor( |
| 118 | + loop=loop, executor=executor, thread_handler=thread_handler, func=func |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +async def wrap_task_context(loop, task_context, awaitable): |
| 123 | + if task_context is None: |
| 124 | + return await awaitable |
| 125 | + |
| 126 | + if isinstance(loop, trio.lowlevel.TrioToken): |
| 127 | + with trio.CancelScope() as scope: |
| 128 | + task_context.append(scope) |
| 129 | + try: |
| 130 | + return await awaitable |
| 131 | + finally: |
| 132 | + task_context.remove(scope) |
| 133 | + if scope.cancelled_caught: |
| 134 | + raise TrioThreadCancelled |
| 135 | + |
| 136 | + return await _asyncio.wrap_task_context(loop, task_context, awaitable) |
0 commit comments