Note that I'm constrained to use Python 2.6. I have a Python 2.6 application that uses a C++ multi-threaded API library built with boost-python. My use-case was simply to execute a Python function callback from a C++ boost thread but despite the many different attempts and researching all the available online resources I haven't found any way that works. All the proposed solutions revolve around a different combination of the functions: Py_Initialize*
, PyEval_InitThreads
, PyGILState_Ensure
, PyGILState_Release
but after trying all possible combinations nothing works in practice e.g.
Therefore, this question: how can I start and run a Python thread from C++? I basically want to: create it, run it with a Python target function object and forget about it.
Is that possible?
Based on the text below from your question:
Run a Python thread from C++? I basically want to: create it, run it with a Python target function object and forget about it.
You may find useful to simply spawn a process using sytem
:
system("python myscript.py")
And if you need to include arguments:
string args = "arg1 arg2 arg3 ... argn"
system("python myscript.py " + args)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With