All Classes Namespaces Functions Variables Typedefs Enumerations Friends
Public Member Functions | Public Attributes | List of all members
ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs > Struct Template Reference

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
 
PythonFutureTaskoperator= (PythonFutureTask const &)=delete
 
 PythonFutureTask (PythonFutureTask &&o)=default
 The move constructor. More...
 
PythonFutureTaskoperator= (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.
 

Detailed Description

template<typename ReturnType, typename... TaskArgs>
struct ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >

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.

Constructor & Destructor Documentation

◆ PythonFutureTask() [1/2]

template<typename ReturnType , typename... TaskArgs>
ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::PythonFutureTask ( std::packaged_task< ReturnType(TaskArgs...)>  task,
std::function< PyObject *(ReturnType)>  pythonConvert,
PyObject *  asyncioEventLoop,
std::launch  launchPolicy = std::launch::async 
)
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.

Parameters
[in]taskthe user-defined C++ task.
[in]pythonConvertC-Python function to convert a C object into a PyObject* representing the result of the task.
[in]asyncioEventLooppointer to a valid Python object containing the event loop that the application is using, to which the Python future will belong to.
[in]launchPolicylaunch policy for the async C++ task.

◆ PythonFutureTask() [2/2]

template<typename ReturnType , typename... TaskArgs>
ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::PythonFutureTask ( PythonFutureTask< ReturnType, TaskArgs > &&  o)
default

The move constructor.

Moves a ucxx::PythonFutureTask to the newly-constructed object.

Parameters
[in]othe object to be moved.

◆ ~PythonFutureTask()

template<typename ReturnType , typename... TaskArgs>
ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::~PythonFutureTask ( )
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.

Member Function Documentation

◆ getFuture()

template<typename ReturnType , typename... TaskArgs>
std::future<ReturnType>& ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::getFuture ( )
inline

Get the C++ future.

Get the underlying C++ future that can be awaited and have its result read.

Returns
The underlying C++ future.

◆ getHandle()

template<typename ReturnType , typename... TaskArgs>
PyObject* ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::getHandle ( )
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.

Warning
The destructor will also destroy the Python future, a pointer taken via this method will cause the object to become invalid.
Exceptions
std::runtime_errorif the object is invalid or has been already released.
Returns
The underlying PyObject* handle.

◆ operator=()

template<typename ReturnType , typename... TaskArgs>
PythonFutureTask& ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::operator= ( PythonFutureTask< ReturnType, TaskArgs > &&  o)
default

The move operator.

Moves a ucxx::PythonFutureTask to the object at the left-hand side.

Parameters
[in]othe object to be moved.

◆ release()

template<typename ReturnType , typename... TaskArgs>
PyObject* ucxx::python::detail::PythonFutureTask< ReturnType, TaskArgs >::release ( )
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.

Exceptions
std::runtime_errorif the object is invalid or has been already released.
Returns
The underlying PyObject* handle.

The documentation for this struct was generated from the following file: