For my C application, I use many interpreters (withh the fonction Py_NewInterpreter()). For my needs, I create many GUI with wxPython and I have problems. Here is My C Code:
{
PyEval_InitThreads();
Py_Initialize();
PyImport_ImportModule("myModule");//it creates a wxApp object and a GUI (mainloop)
......
Py_NewInterpreter();
PyImport_ImportModule("anotherModule");//it should create a new wxApp and a new GUI (with Mainloop)
.....
}
Unfortunately this code does not work. In fact, My C application which use many interpreters is seen by wxPython as a single application, and as wxPython is thread safe, it is not possible to create a second App.
I have to use many interpreters to separate variables and to avoid links between modules.
What could I do to create many GUI in different interpreters (each GUI have to be in a separate environment)???
Thanks.
[Please set your mail client to use word-wrap at a reasonable width.]
mec2paris paris wrote:
For my C application, I use many interpreters (withh the fonction Py_NewInterpreter()). For my needs, I create many GUI with wxPython and I have problems. Here is My C Code:
{
PyEval_InitThreads();
Py_Initialize();PyImport_ImportModule("myModule");//it creates a wxApp object and a GUI (mainloop)
......PyImport_ImportModule("anotherModule");//it should create a new wxApp and a new GUI (with Mainloop)
.....
}
Unfortunately this code does not work. In fact, My C application which use many interpreters is seen by wxPython as a single application, and as wxPython is thread safe, it is not possible to create a second App.I have to use many interpreters to separate variables and to avoid links between modules.
What could I do to create many GUI in different interpreters (each GUI have to be in a separate environment)???
Why do they need to be separate? Why couldn't you have a single wx.App and MainLoop (perhaps running in the C++ code) and then let your Python snippets create their frames to be run in that App and thread? Since GUIs use event dispatching and spend most of their time waiting for an event, there usually isn't much of a need for different parts of the UI to be running in different threads.
On the other hand if you really do need the separate GUIs to be in separate parallel environments, then one approach you may want to consider is to use separate processes.
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
I tested to use a single wxApp and a single MainLoop for my application and it seems to work.
My C application use many threads and many interpreters and I cannot use many processes.
Now, I generate my UIs with the global function wx.CallAfter in the thread of the MainLoop (so in the first interpreter) and with wx.CallAfter I can use data and methods from other interpreters. I may import new modules and change values in the thread of the mainLoop.
Now my C Code becomes :
{
PyEval_InitThreads();
Py_Initialize(); //First interpreter
PyImport_ImportModule(“myModule”);//I create a wxApp + UIs + MainLoop in a new thread
Py_NewInterpreter() //second interpreter
PyImport_ImportModule("anotherModule"); //I create and print my UI with wx.CallAfter
…
}
I would like to know if objects which are created or loaded from an event in the UI of the second interpreter are located in the second interpreter or in the interpreter of the MainLoop.
Thanks.
···
On 10/22/07, Robin Dunn robin@alldunn.com wrote:
[Please set your mail client to use word-wrap at a reasonable width.]
mec2paris paris wrote:
For my C application, I use many interpreters (withh the fonction Py_NewInterpreter()). For my needs, I create many GUI with wxPython and I have problems. Here is My C Code:
{
PyEval_InitThreads();
Py_Initialize();PyImport_ImportModule("myModule");//it creates a wxApp object and a GUI (mainloop) ......
Py_NewInterpreter(); PyImport_ImportModule("anotherModule");//it should create a new wxApp and a new GUI (with Mainloop) .....
}
Unfortunately this code does not work. In fact, My C application which use many interpreters is seen by wxPython as a single application, and as wxPython is thread safe, it is not possible to create a second App.
I have to use many interpreters to separate variables and to avoid links between modules.
What could I do to create many GUI in different interpreters (each GUI have to be in a separate environment)???
Why do they need to be separate? Why couldn’t you have a single wx.App
and MainLoop (perhaps running in the C++ code) and then let your Python
snippets create their frames to be run in that App and thread? SinceGUIs use event dispatching and spend most of their time waiting for an
event, there usually isn’t much of a need for different parts of the UI
to be running in different threads.On the other hand if you really do need the separate GUIs to be in
separate parallel environments, then one approach you may want to
consider is to use separate processes.–
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
Python User wrote:
I tested to use a single wxApp and a single MainLoop for my application and it seems to work.
My C application use many threads and many interpreters and I cannot use many processes.Now, I generate my UIs with the global function wx.CallAfter in the thread of the MainLoop (so in the first interpreter) and with wx.CallAfter I can use data and methods from other interpreters. I may import new modules and change values in the thread of the mainLoop.
Now my C Code becomes :
{
PyEval_InitThreads();
Py_Initialize(); //First interpreterPyImport_ImportModule("myModule");//I create a wxApp + UIs + MainLoop in a new thread
Py_NewInterpreter() //second interpreter
PyImport_ImportModule("anotherModule"); //I create and print my UI with wx.CallAfter
....
}I would like to know if objects which are created or loaded from an event in the UI of the second interpreter are located in the second interpreter or in the interpreter of the MainLoop.
I don't know. Try adding something to __builtins__ or sys.modules or some other place in the 2nd interpreter and then check if it is still there in one of the event handlers in the in the code for the 2nd UI.
BTW, the Python docs say this about using Py_NewInterpreter:
"""
Bugs and caveats: Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn't perfect -- for example, using low-level file operations like os.close() they can (accidentally or maliciously) affect each other's open files. Because of the way extensions are shared between (sub-)interpreters, some extensions may not work properly; this is especially likely when the extension makes use of (static) global variables, or when the extension manipulates its module's dictionary after its initialization. It is possible to insert objects created in one sub-interpreter into a namespace of another sub-interpreter; this should be done with great care to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters, since import operations executed by such objects may affect the wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is a hard-to-fix bug that will be addressed in a future release.)
Also note that the use of this functionality is incompatible with extension modules such as PyObjC and ctypes that use the PyGILState_* APIs (and this is inherent in the way the PyGILState_* functions work). Simple things may work, but confusing behavior will always be near.
"""
Not only is wxPython composed of several extension modules with some global variables, but it also uses the PyGILState_* APIs. So be warned. If you can find another way to accomplish what you need I would suggest doing it.
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!