Skip to content

Integration with asyncio and trio¤

We support integrating tinyio loops into asyncio/trio event loops, and vice-versa.


tinyio.to_asyncio(coro: Coro[~_Return], exception_group: None | bool = None) -> Coroutine[Any, Any, ~_Return] async ¤

Converts a tinyio coroutine into an asyncio coroutine.

Arguments:

  • coro: a tinyio coroutine.
  • exception_group: as the exception_group parameter of tinyio.Loop.run.

Returns:

An asyncio coroutine.

Example

def add_one(x):
    yield tinyio.sleep(1)
    return x + 1

async def foo(x):
    y = await tinyio.to_asyncio(add_one(x))
    return y

asyncio.run(foo(3))

tinyio.from_asyncio(coro: Coroutine[Any, Any, ~_Return]) -> Coro[~_Return] ¤

Converts an asyncio coroutine into a tinyio coroutine.

Arguments:

  • coro: an asyncio coroutine.

Returns:

A tinyio coroutine.

Example

async def add_one(x):
    await asyncio.sleep(1)
    return x + 1

def foo(x):
    y = yield tinyio.from_asyncio(add_one(x))
    return y

tinyio.Loop().run(foo(3))

Warning

Note that the entire asyncio event loop will be ran in a thread. This may lead to surprises if the asyncio and non-asyncio portions interact in non-threadsafe ways.


tinyio.to_trio(coro: Coro[~_Return], exception_group: None | bool = None) -> ~_Return async ¤

Converts a tinyio coroutine into a trio coroutine.

Arguments:

  • coro: a tinyio coroutine.
  • exception_group: as the exception_group parameter of tinyio.Loop.run.

Returns:

A trio coroutine.

Example

def add_one(x):
    yield tinyio.sleep(1)
    return x + 1

async def foo(x):
    y = await tinyio.to_trio(add_one(x))
    return y

trio.run(foo, 3)

tinyio.from_trio(coro: Awaitable[~_Return]) -> Coro[~_Return] ¤

Converts a trio coroutine into a tinyio coroutine.

Uses trio's guest mode to run trio on top of the tinyio event loop.

Arguments:

  • coro: a trio coroutine.

Returns:

A tinyio coroutine.

Example

async def add_one(x):
    await trio.sleep(1)
    return x + 1

def foo(x):
    y = yield tinyio.from_trio(add_one(x))
    return y

tinyio.Loop().run(foo(3))