Panda3D
|
This class represents a thread-safe handle to a promised future result of an asynchronous operation, providing methods to query its status and result as well as register callbacks for this future's completion. More...
#include <pandadoc.hpp>
Public Member Functions | |
__init__ () | |
Initializes the future in the pending state. | |
__init__ (const AsyncFuture) | |
object | addDoneCallback (object fn) |
bool | cancel () |
Cancels the future. | |
bool | cancelled () |
Returns true if the future was cancelled. | |
bool | done () |
Returns true if the future is done or has been cancelled. | |
str | getDoneEvent () |
Returns the event name that will be triggered when the future finishes. | |
output (Ostream out) | |
object | result (object timeout) |
setDoneEvent (str done_event) | |
Sets the event name that will be triggered when the future finishes. | |
setResult (object) | |
wait () | |
Waits until the future is done. | |
wait (double timeout) | |
Waits until the future is done, or until the timeout is reached. | |
![]() | |
TypeHandle | getType () |
int | getTypeIndex () |
Returns the internal index number associated with this object's TypeHandle, a unique number for each different type. | |
bool | isExactType (TypeHandle handle) |
Returns true if the current object is the indicated type exactly. | |
bool | isOfType (TypeHandle handle) |
Returns true if the current object is or derives from the indicated type. | |
![]() | |
int | getRefCount () |
Returns the current reference count. | |
ref () | |
Explicitly increments the reference count. | |
bool | testRefCountIntegrity () |
Does some easy checks to make sure that the reference count isn't completely bogus. | |
bool | testRefCountNonzero () |
Does some easy checks to make sure that the reference count isn't zero, or completely bogus. | |
bool | unref () |
Explicitly decrements the reference count. | |
Static Public Member Functions | |
static object | __await__ () |
static object | __iter__ () |
static object | gather (object args) |
static TypeHandle | getClassType () |
![]() | |
static TypeHandle | getClassType () |
![]() | |
static TypeHandle | getClassType () |
![]() | |
static TypeHandle | getClassType () |
Public Attributes | |
const String | done_event |
Returns the event name that will be triggered when the future finishes. | |
![]() | |
TypeHandle | type |
Returns the TypeHandle representing this object's type. | |
![]() | |
int | ref_count |
The current reference count. | |
This class represents a thread-safe handle to a promised future result of an asynchronous operation, providing methods to query its status and result as well as register callbacks for this future's completion.
An AsyncFuture can be awaited from within a coroutine or task. It keeps track of tasks waiting for this future and automatically reactivates them upon this future's completion.
A task itself is also a subclass of AsyncFuture. Other subclasses are not generally necessary, except to override the function of cancel()
.
Until the future is done, it is "owned" by the resolver thread, though it's still legal for other threads to query its state. When the resolver thread resolves this future using set_result()
, or any thread calls cancel()
, it instantly enters the "done" state, after which the result becomes a read-only field that all threads can access.
When the future returns true for done(), a thread can use cancelled() to determine whether the future was cancelled or get_result() to access the result of the operation. Not all operations define a meaningful result value, so some will always return nullptr.
In Python, the cancelled()
, wait()
and get_result()
methods are wrapped up into a single result()
method which waits for the future to complete before either returning the result or throwing an exception if the future was cancelled. However, it is preferable to use the await
keyword when running from a coroutine, which only suspends the current task and not the entire thread.
This API aims to mirror and be compatible with Python's Future class.
|
static |
__init__ | ( | ) |
Initializes the future in the pending state.
__init__ | ( | const AsyncFuture | ) |
|
static |
object addDoneCallback | ( | object | fn | ) |
bool cancel | ( | ) |
Cancels the future.
Returns true if it was cancelled, or false if the future was already done. Either way, done() will return true after this call returns.
In the case of a task, this is equivalent to remove().
bool cancelled | ( | ) |
Returns true if the future was cancelled.
It is always safe to call this.
bool done | ( | ) |
Returns true if the future is done or has been cancelled.
It is always safe to call this.
|
static |
|
static |
str getDoneEvent | ( | ) |
Returns the event name that will be triggered when the future finishes.
See set_done_event().
output | ( | Ostream | out | ) |
object result | ( | object | timeout | ) |
setDoneEvent | ( | str | done_event | ) |
Sets the event name that will be triggered when the future finishes.
Will not be triggered if the future is cancelled, but it will be triggered for a coroutine task that exits with an exception.
setResult | ( | object | ) |
wait | ( | ) |
Waits until the future is done.
wait | ( | double | timeout | ) |
Waits until the future is done, or until the timeout is reached.
const String done_event |
Returns the event name that will be triggered when the future finishes.
See set_done_event().