RTC Toolkit 5.1.0
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
rtctk::componentFramework::PythonInterpreter Class Referencefinal

Helper class for managing the lifecycle of an embedded Python interpreter. More...

#include <rtctk/componentFramework/pythonInterpreter.hpp>

Classes

class  SetupFailure
 This exception is raised when the Python interpreter cannot be initialised properly. More...
 

Public Member Functions

 PythonInterpreter (bool init_signal_handlers=true, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
 Initialises the embedded Python interpreter.
 
 PythonInterpreter (PyConfig *config, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
 Initialises the embedded Python interpreter using a custom configuration.
 
 PythonInterpreter (const PythonInterpreter &)=delete
 
 PythonInterpreter (PythonInterpreter &&)=delete
 
PythonInterpreteroperator= (const PythonInterpreter &)=delete
 
PythonInterpreteroperator= (PythonInterpreter &&)=delete
 
 ~PythonInterpreter ()=default
 

Detailed Description

Helper class for managing the lifecycle of an embedded Python interpreter.

This class should be instantiated in the main thread, which will initialise an embedded Python interpreter, perform some minimal setup and release the GIL so that other threads can execute Python code. The interpreter will be available as long as the instance of this class is not destroyed, i.e. the lifetime of the Python interpreter is identical to the lifetime of this object.

The following is a canonical example of setting up the Python interpreter to be used in any SRTC component:

void RtcComponentMain(const Args& args) {
}
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:33
Helper class for managing the lifecycle of an embedded Python interpreter.
Definition pythonInterpreter.hpp:61
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
void RtcComponentMain(const rtctk::componentFramework::Args &args)
Main entry point for user code, this method must be implemented by component developers.
Definition main.cpp:43

Then in any other thread it is possible to execute Python code as long as the GIL is acquired. For example, in the ActivityRunning method that is executed in a separate activity thread, one would place an instantiation of the gil_scoped_acquire RAII helper class at the beginning of a scoped block of code that makes invocations to Python using Pybind11:

void BusinessLogic::ActivityRunning(StopToken st) {
// C++ code ...
{
py::gil_scoped_acquire gil;
py::eval(...);
py::print(...);
}
// C++ code ...
}
rad::StopToken StopToken
Definition stopToken.hpp:20

Constructor & Destructor Documentation

◆ PythonInterpreter() [1/4]

rtctk::componentFramework::PythonInterpreter::PythonInterpreter ( bool init_signal_handlers = true,
int argc = 0,
const char *const * argv = nullptr,
bool add_program_dir_to_path = true )
explicit

Initialises the embedded Python interpreter.

Parameters
[in]init_signal_handlersOptional parameter used to skip the registration of signal handlers. (See the Python documentation for details.)
[in]argcOptional parameter used to populate sys.argv. (See the PySys_SetArgvEx documentation for details.)
[in]argvOptional parameter used to populate sys.argv. (See the PySys_SetArgvEx documentation for details.)
[in]add_program_dir_to_pathOptional parameter used to populate sys.path. (See the PySys_SetArgvEx documentation for details.)

◆ PythonInterpreter() [2/4]

rtctk::componentFramework::PythonInterpreter::PythonInterpreter ( PyConfig * config,
int argc = 0,
const char *const * argv = nullptr,
bool add_program_dir_to_path = true )
explicit

Initialises the embedded Python interpreter using a custom configuration.

Parameters
[in]configConfiguration object used to initialise the embedded Python interpreter. (See the PyConfig and Py_InitializeFromConfig documentation for details.)
[in]argcOptional parameter used to populate sys.argv. (See the PySys_SetArgvEx documentation for details.)
[in]argvOptional parameter used to populate sys.argv. (See the PySys_SetArgvEx documentation for details.)
[in]add_program_dir_to_pathOptional parameter used to populate sys.path. (See the PySys_SetArgvEx documentation for details.)

◆ PythonInterpreter() [3/4]

rtctk::componentFramework::PythonInterpreter::PythonInterpreter ( const PythonInterpreter & )
delete

◆ PythonInterpreter() [4/4]

rtctk::componentFramework::PythonInterpreter::PythonInterpreter ( PythonInterpreter && )
delete

◆ ~PythonInterpreter()

rtctk::componentFramework::PythonInterpreter::~PythonInterpreter ( )
default

Member Function Documentation

◆ operator=() [1/2]

PythonInterpreter & rtctk::componentFramework::PythonInterpreter::operator= ( const PythonInterpreter & )
delete

◆ operator=() [2/2]

PythonInterpreter & rtctk::componentFramework::PythonInterpreter::operator= ( PythonInterpreter && )
delete

The documentation for this class was generated from the following files: