1+ """Accelerator module for asyncio
2+ """
13import sys
24from asyncio .events import AbstractEventLoop
35from collections .abc import Awaitable , Callable , Coroutine , Generator
@@ -12,6 +14,19 @@ _TaskYieldType: TypeAlias = Future[object] | None
1214
1315@disjoint_base
1416class Future (Awaitable [_T ]):
17+ """This class is *almost* compatible with concurrent.futures.Future.
18+
19+ Differences:
20+
21+ - result() and exception() do not take a timeout argument and
22+ raise an exception when the future isn't done yet.
23+
24+ - Callbacks registered with add_done_callback() are always called
25+ via the event loop's call_soon_threadsafe().
26+
27+ - This class is not compatible with the wait() and as_completed()
28+ methods in the concurrent.futures package.
29+ """
1530 _state : str
1631 @property
1732 def _exception (self ) -> BaseException | None : ...
@@ -22,24 +37,80 @@ class Future(Awaitable[_T]):
2237 def _log_traceback (self , val : Literal [False ]) -> None : ...
2338 _asyncio_future_blocking : bool # is a part of duck-typing contract for `Future`
2439 def __init__ (self , * , loop : AbstractEventLoop | None = None ) -> None : ...
25- def __del__ (self ) -> None : ...
26- def get_loop (self ) -> AbstractEventLoop : ...
40+ def __del__ (self ) -> None :
41+ """Called when the instance is about to be destroyed.
42+ """
43+ def get_loop (self ) -> AbstractEventLoop :
44+ """Return the event loop the Future is bound to.
45+ """
2746 @property
2847 def _callbacks (self ) -> list [tuple [Callable [[Self ], Any ], Context ]]: ...
29- def add_done_callback (self , fn : Callable [[Self ], object ], / , * , context : Context | None = None ) -> None : ...
30- def cancel (self , msg : Any | None = None ) -> bool : ...
31- def cancelled (self ) -> bool : ...
32- def done (self ) -> bool : ...
33- def result (self ) -> _T : ...
34- def exception (self ) -> BaseException | None : ...
35- def remove_done_callback (self , fn : Callable [[Self ], object ], / ) -> int : ...
36- def set_result (self , result : _T , / ) -> None : ...
37- def set_exception (self , exception : type | BaseException , / ) -> None : ...
38- def __iter__ (self ) -> Generator [Any , None , _T ]: ...
39- def __await__ (self ) -> Generator [Any , None , _T ]: ...
48+ def add_done_callback (self , fn : Callable [[Self ], object ], / , * , context : Context | None = None ) -> None :
49+ """Add a callback to be run when the future becomes done.
50+
51+ The callback is called with a single argument - the future object. If
52+ the future is already done when this is called, the callback is
53+ scheduled with call_soon.
54+ """
55+ def cancel (self , msg : Any | None = None ) -> bool :
56+ """Cancel the future and schedule callbacks.
57+
58+ If the future is already done or cancelled, return False. Otherwise,
59+ change the future's state to cancelled, schedule the callbacks and
60+ return True.
61+ """
62+ def cancelled (self ) -> bool :
63+ """Return True if the future was cancelled.
64+ """
65+ def done (self ) -> bool :
66+ """Return True if the future is done.
67+
68+ Done means either that a result / exception are available, or that the
69+ future was cancelled.
70+ """
71+ def result (self ) -> _T :
72+ """Return the result this future represents.
73+
74+ If the future has been cancelled, raises CancelledError. If the
75+ future's result isn't yet available, raises InvalidStateError. If
76+ the future is done and has an exception set, this exception is raised.
77+ """
78+ def exception (self ) -> BaseException | None :
79+ """Return the exception that was set on this future.
80+
81+ The exception (or None if no exception was set) is returned only if
82+ the future is done. If the future has been cancelled, raises
83+ CancelledError. If the future isn't done yet, raises
84+ InvalidStateError.
85+ """
86+ def remove_done_callback (self , fn : Callable [[Self ], object ], / ) -> int :
87+ """Remove all instances of a callback from the "call when done" list.
88+
89+ Returns the number of callbacks removed.
90+ """
91+ def set_result (self , result : _T , / ) -> None :
92+ """Mark the future done and set its result.
93+
94+ If the future is already done when this method is called, raises
95+ InvalidStateError.
96+ """
97+ def set_exception (self , exception : type | BaseException , / ) -> None :
98+ """Mark the future done and set an exception.
99+
100+ If the future is already done when this method is called, raises
101+ InvalidStateError.
102+ """
103+ def __iter__ (self ) -> Generator [Any , None , _T ]:
104+ """Implement iter(self).
105+ """
106+ def __await__ (self ) -> Generator [Any , None , _T ]:
107+ """Return an iterator to be used in await expression.
108+ """
40109 @property
41110 def _loop (self ) -> AbstractEventLoop : ...
42- def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
111+ def __class_getitem__ (cls , item : Any , / ) -> GenericAlias :
112+ """See PEP 585
113+ """
43114
44115if sys .version_info >= (3 , 12 ):
45116 _TaskCompatibleCoro : TypeAlias = Coroutine [Any , Any , _T_co ]
52123# and `asyncio.Task.set_result()` always raises.
53124@disjoint_base
54125class Task (Future [_T_co ]): # type: ignore[type-var] # pyright: ignore[reportInvalidTypeArguments]
126+ """A coroutine wrapped in a Future.
127+ """
55128 if sys .version_info >= (3 , 12 ):
56129 def __init__ (
57130 self ,
@@ -86,27 +159,118 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn
86159 if sys .version_info >= (3 , 12 ):
87160 def get_context (self ) -> Context : ...
88161
89- def get_stack (self , * , limit : int | None = None ) -> list [FrameType ]: ...
90- def print_stack (self , * , limit : int | None = None , file : TextIO | None = None ) -> None : ...
162+ def get_stack (self , * , limit : int | None = None ) -> list [FrameType ]:
163+ """Return the list of stack frames for this task's coroutine.
164+
165+ If the coroutine is not done, this returns the stack where it is
166+ suspended. If the coroutine has completed successfully or was
167+ cancelled, this returns an empty list. If the coroutine was
168+ terminated by an exception, this returns the list of traceback
169+ frames.
170+
171+ The frames are always ordered from oldest to newest.
172+
173+ The optional limit gives the maximum number of frames to
174+ return; by default all available frames are returned. Its
175+ meaning differs depending on whether a stack or a traceback is
176+ returned: the newest frames of a stack are returned, but the
177+ oldest frames of a traceback are returned. (This matches the
178+ behavior of the traceback module.)
179+
180+ For reasons beyond our control, only one stack frame is
181+ returned for a suspended coroutine.
182+ """
183+ def print_stack (self , * , limit : int | None = None , file : TextIO | None = None ) -> None :
184+ """Print the stack or traceback for this task's coroutine.
185+
186+ This produces output similar to that of the traceback module,
187+ for the frames retrieved by get_stack(). The limit argument
188+ is passed to get_stack(). The file argument is an I/O stream
189+ to which the output is written; by default output is written
190+ to sys.stderr.
191+ """
91192 if sys .version_info >= (3 , 11 ):
92- def cancelling (self ) -> int : ...
93- def uncancel (self ) -> int : ...
193+ def cancelling (self ) -> int :
194+ """Return the count of the task's cancellation requests.
195+
196+ This count is incremented when .cancel() is called
197+ and may be decremented using .uncancel().
198+ """
199+ def uncancel (self ) -> int :
200+ """Decrement the task's count of cancellation requests.
201+
202+ This should be used by tasks that catch CancelledError
203+ and wish to continue indefinitely until they are cancelled again.
204+
205+ Returns the remaining number of cancellation requests.
206+ """
207+
208+ def __class_getitem__ (cls , item : Any , / ) -> GenericAlias :
209+ """See PEP 585
210+ """
211+
212+ def get_event_loop () -> AbstractEventLoop :
213+ """Return an asyncio event loop.
214+
215+ When called from a coroutine or a callback (e.g. scheduled with
216+ call_soon or similar API), this function will always return the
217+ running event loop.
218+
219+ If there is no running event loop set, the function will return
220+ the result of `get_event_loop_policy().get_event_loop()` call.
221+ """
222+ def get_running_loop () -> AbstractEventLoop :
223+ """Return the running event loop. Raise a RuntimeError if there is none.
224+
225+ This function is thread-specific.
226+ """
227+ def _set_running_loop (loop : AbstractEventLoop | None , / ) -> None :
228+ """Set the running event loop.
229+
230+ This is a low-level function intended to be used by event loops.
231+ This function is thread-specific.
232+ """
233+ def _get_running_loop () -> AbstractEventLoop :
234+ """Return the running event loop or None.
235+
236+ This is a low-level function intended to be used by event loops.
237+ This function is thread-specific.
238+ """
239+ def _register_task (task : Task [Any ]) -> None :
240+ """Register a new task in asyncio as executed by loop.
241+
242+ Returns None.
243+ """
244+ def _unregister_task (task : Task [Any ]) -> None :
245+ """Unregister a task.
246+
247+ Returns None.
248+ """
249+ def _enter_task (loop : AbstractEventLoop , task : Task [Any ]) -> None :
250+ """Enter into task execution or resume suspended task.
251+
252+ Task belongs to loop.
253+
254+ Returns None.
255+ """
256+ def _leave_task (loop : AbstractEventLoop , task : Task [Any ]) -> None :
257+ """Leave task execution or suspend a task.
94258
95- def __class_getitem__ ( cls , item : Any , / ) -> GenericAlias : .. .
259+ Task belongs to loop .
96260
97- def get_event_loop () -> AbstractEventLoop : ...
98- def get_running_loop () -> AbstractEventLoop : ...
99- def _set_running_loop (loop : AbstractEventLoop | None , / ) -> None : ...
100- def _get_running_loop () -> AbstractEventLoop : ...
101- def _register_task (task : Task [Any ]) -> None : ...
102- def _unregister_task (task : Task [Any ]) -> None : ...
103- def _enter_task (loop : AbstractEventLoop , task : Task [Any ]) -> None : ...
104- def _leave_task (loop : AbstractEventLoop , task : Task [Any ]) -> None : ...
261+ Returns None.
262+ """
105263
106264if sys .version_info >= (3 , 12 ):
107- def current_task (loop : AbstractEventLoop | None = None ) -> Task [Any ] | None : ...
265+ def current_task (loop : AbstractEventLoop | None = None ) -> Task [Any ] | None :
266+ """Return a currently executed task.
267+ """
108268
109269if sys .version_info >= (3 , 14 ):
110270 def future_discard_from_awaited_by (future : Future [Any ], waiter : Future [Any ], / ) -> None : ...
111- def future_add_to_awaited_by (future : Future [Any ], waiter : Future [Any ], / ) -> None : ...
112- def all_tasks (loop : AbstractEventLoop | None = None ) -> set [Task [Any ]]: ...
271+ def future_add_to_awaited_by (future : Future [Any ], waiter : Future [Any ], / ) -> None :
272+ """Record that `fut` is awaited on by `waiter`.
273+ """
274+ def all_tasks (loop : AbstractEventLoop | None = None ) -> set [Task [Any ]]:
275+ """Return a set of all tasks for the loop.
276+ """
0 commit comments