Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start a Python thread FROM C++?

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.

  • Embedding Python in multi-threaded C++ applications with code here
  • PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseum)

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?

like image 906
SkyWalker Avatar asked Nov 08 '22 04:11

SkyWalker


1 Answers

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)
like image 183
otorrillas Avatar answered Nov 15 '22 07:11

otorrillas