Hi!

I try to embed python into a Qt C++ application.

I don't need any speciality, just run some prepared scripts from time to time. E.g. I have a function which should do some plotting through pylab.

When I call this function ("run", see below) from the main (GUI) thread, everything works fine and as expected. But as soon as I spawn a new thread to run this function in it, it stops to work ('hangs' forever?)

Strangely, the problem only shows up when I call 'import pylab'. It seems everything else which I tested (all other non-pylab related python code) works fine even when executed from any thread.

I need to run the python scripts in a different thread than the main thread (which also controls the GUI) to keep the application responsive while running longer scripts.


int run() {
  Py_Initialize();
  PyGILState_STATE state =  PyGILState_Ensure();
  PyRun_SimpleString("import pylab; pylab.plot([1,2]); pylab.show();");
  PyGILState_Release(state);
  Py_Finalize();
}

// the function from the Qt GUI-thread (main thread) works fine
// (with any python string in PyRun_SimpleString() )
run();

// but this blocks forever at 'import pylab'
std::thread t = std::thread(run);
t.join();


(I call Py_Initialize only once in the program)

Could this be a threading issue? I don't think so, because even when the qt app uses multiple threads, only one of it accesses python variables. It seems to me, pylab does not like it when another thread controls the GUIs!?! Is there a work around/fix? What do I miss?


I tried putting Py_Initialize/Py_Finalize also into the main thread, but it didn't made any difference.


Many thanks for any hints!


Regards,
Jan


_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to