asyncx.EventLoopThread
- class asyncx.EventLoopThread(loop_policy=None, daemon=False, start=False)[source]
An event loop thread that provides thread-safe utility functions.
Note
The thread cannot be started more than once because of the constraints of
threading.Thread. The class raisesRuntimeErrorif it is already terminated.Example
>>> async def _get_ident() -> int: ... return threading.get_ident() ... >>> thread = EventLoopThread() >>> with thread: ... main, sub = await asyncio.gather( ... _get_ident(), ... thread.run_coroutine(_get_ident()), ... ) >>> main == sub False
- Parameters
loop_policy (Optional[asyncio.AbstractEventLoopPolicy]) –
daemon (bool) –
start (bool) –
- Return type
None
- __init__(loop_policy=None, daemon=False, start=False)[source]
Creates a new event loop thread.
- Parameters
loop_policy (Optional[asyncio.events.AbstractEventLoopPolicy]) – A
asyncio.AbstractEventLoopPolicyobject to be used for creating a new event loop. IfNoneis specified,asyncio.get_event_loop_policy()is used to get the policy.daemon (bool) –
If
Trueis specified, the thread is created withdaemon=True. Refer to threading.Thread.daemon for further details.start (bool) – If
Trueis specified, the thread is started immediately.
- Return type
None
Methods
__init__([loop_policy, daemon, start])Creates a new event loop thread.
getName()get_loop()Get an event loop of the running thread.
isAlive()Return whether the thread is alive.
isDaemon()is_alive()Return whether the thread is alive.
join([timeout])Wait until the thread terminates.
run()Method representing the thread's activity.
run_coroutine(coro, *[, loop])Submit a coroutine in a new thread and waits for its completion in a given
loop.run_coroutine_concurrent(coro)Submit a coroutine in the event loop.
setDaemon(daemonic)setName(name)shutdown([join])Shutdown the running event loop to terminate the thread.
start()Start the thread and wait for a new event loop to be ready.
Attributes
daemonA boolean value indicating whether this thread is a daemon thread.
identThread identifier of this thread or None if it has not been started.
Get an event loop of the running thread.
nameA string used for identification purposes only.
native_idNative integral thread ID of this thread, or None if it has not been started.
- get_loop()[source]
Get an event loop of the running thread.
The thread must be running. The method will raise
RuntimeErrorif the thread is not running.See also
Use
EventLoopThread.loopif you need an accessor to the loop.- Return type
asyncio.events.AbstractEventLoop
- property loop: asyncio.events.AbstractEventLoop
Get an event loop of the running thread.
The property just returns the return value of
EventLoopThread.get_loop().
- run_coroutine(coro, *, loop=None)[source]
Submit a coroutine in a new thread and waits for its completion in a given
loop.- Parameters
coro (Coroutine[Any, Any, TReturn]) – A Coroutine object to run.
loop (Optional[asyncio.AbstractEventLoop]) – An event loop to wait for the completion of
coro.
- Returns
A
asyncio.Futureobject that returns the execution result of a given coroutine.- Return type
asyncio.Future[TReturn]
- run_coroutine_concurrent(coro)[source]
Submit a coroutine in the event loop.
- Parameters
coro (Coroutine[Any, Any, TReturn]) – A Coroutine object to run.
- Returns
A
concurrent.future.Futureobject that returns the execution result of a given coroutine.- Return type
concurrent.futures.Future[TReturn]
- shutdown(join=True)[source]
Shutdown the running event loop to terminate the thread.
If the event loop is not running, it returns immediately.
- Parameters
join (bool) – If True is specified, the method waits for the thread to be terminated.
- Raises
RumtimeError – The method raises a
RuntimeErrorifjoin = Trueand the method is called by the same thread asself.loopin order to avoid a deadlock. See threading.Thread.join for further details.- Return type
None