[QUESTION] How to move PyThreadState
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
How would one go about moving the PyThreadState created in the main thread to the _runThread in the code snippet below. It is described as possible here, but the there is no example provided, and I can't make sense of the nanogui implementation either. Simply constructing the gil_scoped_release with disassoc = true doesn't seem to do the job.
class PythonScriptRunner
{
public:
PythonScriptRunner()
{
py::module sys = py::module::import("sys");
sys.attr("path").attr("insert")(1, "customModulePath");
_module = py::module::import("customModuleName");
_gil.emplace();
}
void Execute()
{
pybind11::gil_scoped_acquire acq;
_module.attr("function")();
}
~PythonScriptRunner()
{
_gil.reset()
}
private:
pybind11::scoped_interpreter _guard;
pybind11::module _module;
std::optional<pybind11::gil_scoped_release> _gil;
}
class PythonSubprocess
{
public:
PythonSubprocess()
{
_runner = std::make_unique<PythonScriptRunner>();
}
void Run()
{
_runThread = std::thread(&PythonSubprocess::RunFunction, this);
}
void Stop()
{
_runThread.join();
}
private:
void RunFunction()
{
_runner->Execute();
}
private:
std::unique_ptr<PythonScriptRunner> _runner;
std::thread _runThread {};
};
int main()
{
PythonSubprocess sub;
sub.Run();
sub.Stop();
return 0;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with include/pybind11/gil.h, especially the documentation linked in the issue, and compare it with the nanogui implementation mentioned there. Determine whether the provided PythonScriptRunner and PythonSubprocess entry points can move PyThreadState to _runThread, then document a working example or clarify the required usage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100