spyder.api.asyncdispatcher#
Spyder AsyncDispatcher API.
This module provides an class decorator AsyncDispatcher to run coroutines on dedicated async loops, including utilities for patching loops, managing concurrency tasks, and executing callbacks safely within Qt applications.
Functions
|
Submit a coroutine object to a given event loop. |
Classes
Decorator to run a coroutine in a specific event loop. |
|
Represents the result of an asynchronous computation. |
- class spyder.api.asyncdispatcher.AsyncDispatcher(*, loop: Hashable | AbstractEventLoop | None = ..., early_return: Literal[True] = ..., return_awaitable: Literal[False] = ...)[source]#
- class spyder.api.asyncdispatcher.AsyncDispatcher(*, loop: Hashable | AbstractEventLoop | None = ..., early_return: Literal[True] = ..., return_awaitable: Literal[True] = ...)
- class spyder.api.asyncdispatcher.AsyncDispatcher(*, loop: Hashable | AbstractEventLoop | None = ..., early_return: Literal[False] = ..., return_awaitable: Literal[False] = ...)
- class spyder.api.asyncdispatcher.AsyncDispatcher(*, loop: Hashable | AbstractEventLoop | None = ..., early_return: Literal[False] = ..., return_awaitable: Literal[True] = ...)
Bases:
Generic[_RT]Decorator to run a coroutine in a specific event loop.
- classmethod get_event_loop(loop_id: Hashable | AbstractEventLoop | None = None) AbstractEventLoop[source]#
Get the event loop to run the coroutine.
If the loop is not running, it will be started in a new thread and managed by the AsyncDispatcher.
- Parameters:
loop_id (LoopID, optional (default: None)) – The event loop to be used, by default gets the current thread event loop.
Notes
If a hashable is provided, it will be used to identify the loop in the AsyncDispatcher.
If an event loop is provided, it will be used as the event loop in the AsyncDispatcher.
- Returns:
The event loop to be used.
- Return type:
AbstractEventLoop
- classmethod join(timeout: float | None = None)[source]#
Close all running loops and join the threads.
- static QtSlot(func: Callable[[_P], None]) Callable[[_P], None][source]#
Mark a function to be executed inside the main qt loop.
Set the DispatcherFuture.QT_SLOT_ATTRIBUTE attribute to the function to mark it as a slot to be executed in the main Qt loop.
- Parameters:
func (Callable) – The function to be marked.
- Returns:
The marked function.
- Return type:
Callable
- class spyder.api.asyncdispatcher.DispatcherFuture[source]#
Bases:
Future,Generic[_T]Represents the result of an asynchronous computation.
This class is a subclass of concurrent.Future that adds a connect method to allow attaching callbacks to be executed in the main Qt loop.
- result(timeout: float | None = None) _T[source]#
Return the result of the call that the future represents.
- Parameters:
timeout (float | None) – The number of seconds to wait for the result. If None, then wait indefinitely.
- Returns:
The result of the call that the future represents.
- Return type:
- Raises:
CancelledError – If the future was cancelled.
TimeoutError – If the future didn’t finish executing before the given timeout.
Exception – Exception raised by the call that the future represents.
- connect(fn: Callable[[DispatcherFuture[_T]], None])[source]#
Attaches a callable that will be called when the future finishes.
The callable will be called by a thread in the same process in which it was added if the it was not marked with DispatherFuture.QT_SLOT_ATTRIBUTE.
If the future has already completed or been cancelled then the callable will be called immediately. These callables are called in the order that they were added.
- Parameters:
fn (Callable) – A callable that will be called with this future’s as its only argument when the future completes.
- spyder.api.asyncdispatcher.run_coroutine_threadsafe(coro: Coroutine[_T, None, _RT], loop: AbstractEventLoop) DispatcherFuture[_RT][source]#
Submit a coroutine object to a given event loop.
- Parameters:
coro (Coroutine) – The coroutine object to be submitted.
loop (AbstractEventLoop) – The event loop to run the coroutine.
- Returns:
A future object representing the result of the coroutine.
- Return type:
- Raises:
TypeError – If the object is not a coroutine.