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 raises RuntimeError if 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.AbstractEventLoopPolicy object to be used for creating a new event loop. If None is specified, asyncio.get_event_loop_policy() is used to get the policy.

  • daemon (bool) –

    If True is specified, the thread is created with daemon=True. Refer to threading.Thread.daemon for further details.

  • start (bool) – If True is 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

daemon

A boolean value indicating whether this thread is a daemon thread.

ident

Thread identifier of this thread or None if it has not been started.

loop

Get an event loop of the running thread.

name

A string used for identification purposes only.

native_id

Native 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 RuntimeError if the thread is not running.

See also

Use EventLoopThread.loop if 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.Future object 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.Future object 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 RuntimeError if join = True and the method is called by the same thread as self.loop in order to avoid a deadlock. See threading.Thread.join for further details.

Return type

None

start()[source]

Start the thread and wait for a new event loop to be ready.

If the thread is already started, it returns immediately.

Raises

RuntimeError – If the thread is already terminated.

Return type

None