forked from goodboy/tractor
1
0
Fork 0

Propagate any spawned `asyncio` task error upwards

This should mostly maintain top level SC principles for any task spawned
using `tractor.to_asyncio.run()`. When the `asyncio` task completes make
sure to cancel the pertaining `trio` cancel scope and raise any error
that may have resulted.

Resolves #120
drop-trip-update-trio
Tyler Goodlet 2020-06-28 22:44:16 -04:00
parent 8434c76451
commit ebaf129283
2 changed files with 43 additions and 17 deletions

View File

@ -12,9 +12,6 @@ from .log import get_console_log, get_logger
from . import _state from . import _state
__all__ = ('run',)
log = get_logger(__name__) log = get_logger(__name__)

View File

@ -13,22 +13,26 @@ from typing import (
import trio import trio
from ._state import current_actor
__all__ = ['run']
async def _invoke( async def _invoke(
from_trio, from_trio: trio.abc.ReceiveChannel,
to_trio, to_trio: asyncio.Queue,
coro coro: Awaitable,
) -> Union[AsyncGenerator, Awaitable]: ) -> Union[AsyncGenerator, Awaitable]:
"""Await or stream awaiable object based on type into """Await or stream awaiable object based on type into
``trio`` memory channel. ``trio`` memory channel.
""" """
async def stream_from_gen(c): async def stream_from_gen(c):
async for item in c: async for item in c:
to_trio.put_nowait(item) to_trio.send_nowait(item)
to_trio.put_nowait
async def just_return(c): async def just_return(c):
to_trio.put_nowait(await c) to_trio.send_nowait(await c)
if inspect.isasyncgen(coro): if inspect.isasyncgen(coro):
return await stream_from_gen(coro) return await stream_from_gen(coro)
@ -36,7 +40,6 @@ async def _invoke(
return await coro return await coro
# TODO: make this some kind of tractor.to_asyncio.run()
async def run( async def run(
func: Callable, func: Callable,
qsize: int = 2**10, qsize: int = 2**10,
@ -45,6 +48,8 @@ async def run(
"""Run an ``asyncio`` async function or generator in a task, return """Run an ``asyncio`` async function or generator in a task, return
or stream the result back to ``trio``. or stream the result back to ``trio``.
""" """
assert current_actor()._infected_aio
# ITC (inter task comms) # ITC (inter task comms)
from_trio = asyncio.Queue(qsize) from_trio = asyncio.Queue(qsize)
to_trio, from_aio = trio.open_memory_channel(qsize) to_trio, from_aio = trio.open_memory_channel(qsize)
@ -55,16 +60,40 @@ async def run(
coro = func(**kwargs) coro = func(**kwargs)
cancel_scope = trio.CancelScope()
# start the asyncio task we submitted from trio # start the asyncio task we submitted from trio
# TODO: try out ``anyio`` asyncio based tg here # TODO: try out ``anyio`` asyncio based tg here
asyncio.create_task(_invoke(from_trio, to_trio, coro)) task = asyncio.create_task(_invoke(from_trio, to_trio, coro))
err = None
# XXX: I'm not sure this actually does anything...
def cancel_trio(task):
"""Cancel the calling ``trio`` task on error.
"""
nonlocal err
err = task.exception()
cancel_scope.cancel()
task.add_done_callback(cancel_trio)
# determine return type async func vs. gen # determine return type async func vs. gen
if inspect.isasyncgen(coro): if inspect.isasyncgen(coro):
await from_aio.get() # simple async func
elif inspect.iscoroutine(coro): async def result():
async def gen(): with cancel_scope:
async for tick in from_aio: return await from_aio.get()
yield tuple(tick) if cancel_scope.cancelled_caught and err:
raise err
return gen() elif inspect.iscoroutine(coro):
# asycn gen
async def result():
with cancel_scope:
async with from_aio:
async for item in from_aio:
yield item
if cancel_scope.cancelled_caught and err:
raise err
return result()