A bridge of C++ and Python futures. More...
#include <python_future_task.h>
Public Member Functions | |
PythonFutureTask (std::packaged_task< ReturnType(TaskArgs...)> task, std::function< PyObject *(ReturnType)> pythonConvert, PyObject *asyncioEventLoop, std::launch launchPolicy=std::launch::async) | |
Construct a Python future backed by C++ std::packaged_task . More... | |
PythonFutureTask (const PythonFutureTask &)=delete | |
PythonFutureTask & | operator= (PythonFutureTask const &)=delete |
PythonFutureTask (PythonFutureTask &&o)=default | |
The move constructor. More... | |
PythonFutureTask & | operator= (PythonFutureTask &&o)=default |
The move operator. More... | |
~PythonFutureTask () | |
Python future destructor. More... | |
std::future< ReturnType > & | getFuture () |
Get the C++ future. More... | |
PyObject * | getHandle () |
Get the underlying future PyObject* handle but does not release ownership. More... | |
PyObject * | release () |
Get the underlying future PyObject* handle and release ownership. More... | |
Public Attributes | |
std::packaged_task< ReturnType(TaskArgs...)> | _task {} |
The user-defined C++ task to run. | |
std::function< PyObject *(ReturnType)> | _pythonConvert {} |
Function to convert the C++ result into Python value. | |
PyObject * | _asyncioEventLoop {} |
The handle to the Python asyncio event loop. | |
PyObject * | _handle {} |
The handle to the Python future. | |
std::future< ReturnType > | _future {} |
The C++ future containing the task result. | |
A bridge of C++ and Python futures.
A bridge of C++ and Python futures, notifying the Python future handled by this object when the underlying C++ future completes.
|
inlineexplicit |
Construct a Python future backed by C++ std::packaged_task
.
Construct a future object that receives a user-defined C++ std::packaged_task
which runs asynchronously using an internal std::async
that ultimately notifies a Python future that can be awaited in Python code.
Note that this call will take the Python GIL and requires that the current thread have an asynchronous event loop set.
[in] | task | the user-defined C++ task. |
[in] | pythonConvert | C-Python function to convert a C object into a PyObject* representing the result of the task. |
[in] | asyncioEventLoop | pointer to a valid Python object containing the event loop that the application is using, to which the Python future will belong to. |
[in] | launchPolicy | launch policy for the async C++ task. |
|
default |
The move constructor.
Moves a ucxx::PythonFutureTask
to the newly-constructed object.
[in] | o | the object to be moved. |
|
inline |
Python future destructor.
Register the handle for future garbage collection. It is unsafe to require the GIL here since the exact time the destructor is called may be unpredictable w.r.t. the Python application, and thus requiring the GIL here may result in deadlocks. The application is thus responsible to ensure PythonFutureTaskCollector::push()
is regularly called and ultimately responsible for cleaning up before terminating, otherwise a resource leakage may occur.
|
inline |
Get the C++ future.
Get the underlying C++ future that can be awaited and have its result read.
|
inline |
Get the underlying future PyObject*
handle but does not release ownership.
Get the underlying PyObject*
handle without releasing ownership. This can be useful for example for logging, where we want to see the address of the pointer but do not want to transfer ownership.
std::runtime_error | if the object is invalid or has been already released. |
PyObject*
handle.
|
default |
The move operator.
Moves a ucxx::PythonFutureTask
to the object at the left-hand side.
[in] | o | the object to be moved. |
|
inline |
Get the underlying future PyObject*
handle and release ownership.
Get the underlying PyObject*
handle releasing ownership. This should be used when the future needs to be permanently transferred to Python code. After calling this method the object becomes invalid for any other uses.
std::runtime_error | if the object is invalid or has been already released. |
PyObject*
handle.