asyncx.run_coroutine_in_thread

asyncx.run_coroutine_in_thread(coro, *, loop=None)[source]

Submit a coroutine in a new thread and waits for its completion in a given loop.

Example

>>> async def _get_ident() -> int:
...     return threading.get_ident()
...
>>> main, sub = await asyncio.gather(
...     _get_ident(),
...     asyncx.run_coroutine_in_thread(_get_ident()),
... )
>>> main == sub
False
Parameters
  • coro (Coroutine[Any, Any, TReturn]) – A coroutine to run in a new thread.

  • loop (Optional[asyncio.AbstractEventLoop]) – An event loop to wait for the completion of coro.

Returns

A asyncio.Future object that returns the execution result of a given coroutine.

Return type

asyncio.Future[TReturn]