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:
}
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:32
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.
Definition pythonInterpreter.cpp:22
void RtcComponentMain(const rtctk::componentFramework::Args &args)
Main entry point for user code, this method must be implemented by component developers.
Definition main.cpp:42
void RunAsRtcComponent(const Args &args, BLF factory)
RTC Component runner function, needed to run custom BusinessLogic as RTC Component.
Definition rtcComponentMain.hpp:73
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) {
{
py::gil_scoped_acquire gil;
py::eval(...);
py::print(...);
}
}
rad::StopToken StopToken
Definition stopToken.hpp:19