简体   繁体   中英

Why can't I use WX from a child process?

Normally I can use wx.GetApp() no problem:

>>> import wx
>>> wx.__version__
'2.8.12.0'
>>> wx.GetApp()
<wx._core.App; proxy of <Swig Object of type 'wxPyApp *' at 0x67dd690> >

However, from a subprocess wx.GetApp() returns None

from   multiprocessing import Process
import wx

def main():
    process = Process( target=target )
    process.start()
    process.join()

def target():
    print 'wx.GetApp() = %s' % wx.GetApp()

The above codes prints wx.GetApp() = None .

Why does wx not work the same in a child process?

Because all of the global data and shared libraries associated with wxWidgets is not inherited by the child process, and since most of the wx objects are really wrapped C++ objects Python's multiprocess module is not able to pass them back and forth. The best you can do is create a new wx.App and any other GUI elements needed in the child processes. If the parent process needs a GUI then it would be better to delay creating it there until after the child processes have been started, if possible.

If you simply need your child processes to update or otherwise influence the main GUI of the parent and don't need to have their own separate GUI, then that can be done similarly to how you could do it in a single process with multiple threads. The child processes can send messages to the parent process as needed with something like multiprocessing.Queue , and the parent can catch them and use wx.CallAfter to invoke the code that will update the GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM