AsyncFuture
from panda3d.core import AsyncFuture
- class AsyncFuture
Bases:
TypedReferenceCount
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 callscancel()
, 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()
andget_result()
methods are wrapped up into a singleresult()
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 theawait
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.
New in version 1.10.0.
Inheritance diagram
- __init__(*args, **kwargs)
- addDoneCallback()
C++ Interface: add_done_callback(const AsyncFuture self, object fn)
- add_done_callback()
C++ Interface: add_done_callback(const AsyncFuture self, object fn)
- cancel()
C++ Interface: cancel(const AsyncFuture self)
- /**
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.
Please note that calling this is not a guarantee that the operation
corresponding this future does not run. It could already be in the process
of running, or perhaps not respond to a cancel signal. All this guarantees
is that the future is marked as done when this call returns.
In the case of a task, this is equivalent to remove().
*/
- cancelled()
C++ Interface: cancelled(AsyncFuture self)
- /**
Returns true if the future was cancelled. It is always safe to call this.
*/
- done()
C++ Interface: done(AsyncFuture self)
- /**
Returns true if the future is done or has been cancelled. It is always
safe to call this.
*/
- done_event
- gather()
Creates a new future that returns
done()
when all of the contained futures are done.Calling
cancel()
on the returned future will result in all contained futures that have not yet finished to be cancelled.
- getClassType()
C++ Interface: get_class_type()
- getDoneEvent()
C++ Interface: get_done_event(AsyncFuture self)
- /**
Returns the event name that will be triggered when the future finishes.
See set_done_event().
*/
- get_class_type()
C++ Interface: get_class_type()
- get_done_event()
C++ Interface: get_done_event(AsyncFuture self)
- /**
Returns the event name that will be triggered when the future finishes.
See set_done_event().
*/
- result()
C++ Interface: result(AsyncFuture self, object timeout)
- setDoneEvent()
C++ Interface: set_done_event(const AsyncFuture self, 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()
C++ Interface: set_result(const AsyncFuture self, object param0)
- /**
Sets this future’s result. Can only be called if done() returns false.
*/
- /**
Sets this future’s result as a generic TypedObject.
*/
- /**
Sets this future’s result. Can only be done while the future is not done.
Calling this marks the future as done and schedules the done callbacks.
This variant takes two pointers; the second one is only set if this object
inherits from ReferenceCount, so that a reference can be held.
Assumes the manager’s lock is not held.
*/
- set_done_event()
C++ Interface: set_done_event(const AsyncFuture self, 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.
*/
- set_result()
C++ Interface: set_result(const AsyncFuture self, object param0)
- /**
Sets this future’s result. Can only be called if done() returns false.
*/
- /**
Sets this future’s result as a generic TypedObject.
*/
- /**
Sets this future’s result. Can only be done while the future is not done.
Calling this marks the future as done and schedules the done callbacks.
This variant takes two pointers; the second one is only set if this object
inherits from ReferenceCount, so that a reference can be held.
Assumes the manager’s lock is not held.
*/
- shield()
C++ Interface: shield(AsyncFuture future)
- /**
Creates a new future that shields the given future from cancellation.
Calling cancel() on the returned future will not affect the given future.
*/
- upcastToTypedReferenceCount()
C++ Interface: upcast_to_TypedReferenceCount(const AsyncFuture self)
upcast from AsyncFuture to TypedReferenceCount
- upcast_to_TypedReferenceCount()
C++ Interface: upcast_to_TypedReferenceCount(const AsyncFuture self)
upcast from AsyncFuture to TypedReferenceCount
- wait()
C++ Interface: wait(const AsyncFuture self) wait(const AsyncFuture self, double timeout)