[pygtk] Can't run PyGTK more than once from embedded Python
Romain Behar
romainbehar at yahoo.com
Wed Jan 11 19:47:29 WST 2006
I've finally been able to run PyGTK more than once, it
was just missing the Py_Finalize() function to do
proper cleanup. I've put the code below.
Don't be scared: the application is C++, it uses Gtk+
through the gtkmm wrapper and uses Python as scripting
engine; we're now able to use PyGTK too to enhance our
scripts and user experience.
There remains one bug though, but it probably comes
from the bad thread handling:
* Launch the application
* Run a PyGTK script that opens a window (keep it
open)
* Open a new document from the menu
* Run another Python script from this new document
(using PyGtk or not)
* Close the PyGTK window from the first document ->
crash
But this is a Python issue not a PyGTK one.
Many thanks for your help.
Cheers,
Romain
#include <Python.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char* function = "import
pygtk\npygtk.require('2.0')\nimport gtk\n\nclass
Base:\n\tdef __init__(self):\n\t\tself.window =
gtk.Window(gtk.WINDOW_TOPLEVEL)\n\t\tself.window.connect('destroy',
gtk.main_quit)\n\t\tself.window.show()\n\n\tdef
main(self):\n\t\tgtk.main()\n\nprint __name__\nif
__name__ == \"__main__\":\n\tbase =
Base()\n\tbase.main()\n\n";
int returncode = 0;
Py_Initialize();
//PyEval_InitThreads();
PyThreadState* global_state = PyThreadState_Get();
PyThreadState* interpreter = Py_NewInterpreter();
PyThreadState_Swap(global_state);
PyThreadState_Swap(interpreter);
returncode = PyRun_SimpleString(function);
PyThreadState_Swap(global_state);
fprintf(stderr, "Code : %d\n", returncode);
PyThreadState_Swap(interpreter);
Py_EndInterpreter(interpreter);
PyThreadState_Swap(global_state);
if(Py_IsInitialized())
Py_Finalize();
/*
----------------------------------------------------
*/
Py_Initialize();
PyThreadState* global_state2 = PyThreadState_Get();
PyThreadState* interpreter2 = Py_NewInterpreter();
PyThreadState_Swap(global_state2);
PyThreadState_Swap(interpreter2);
returncode = PyRun_SimpleString(function);
PyThreadState_Swap(global_state2);
fprintf(stderr, "Code : %d\n", returncode);
PyThreadState_Swap(interpreter2);
Py_EndInterpreter(interpreter2);
PyThreadState_Swap(global_state2);
if(Py_IsInitialized())
Py_Finalize();
return 0;
}
> Having multiple pygtk instances means that
> you have multiple Gtk event loops. This
> is not good. You are going to have weird
> problems in the UI event systems (things
> like cut and paste between windows, close
> systems, etc.).
>
> You really might want to rethink this
> architecture.
>
> You might want to make a global Python
> interpreter which initializes and then
> calls your current C code as an extension
> module. Then, the C code can call *back*
> into the global Python interpreter when
> it needs Python.
>
> It shouldn't be that much extra code and
> it means that you don't have to rewrite
> the entire application.
>
> -a
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the pygtk
mailing list