Python multiprocessing and wxPython working togeth

2019-08-15 08:50发布

问题:

I've got the following problem:

I've written a script which is running up to four processes at the same time. Works like a charm when using it via command line. Then I made the decision to write a GUI with wxPython and I was quickly figuring out that the GUI and the script need to run in different processes so both stay usable while the other is doing something. (i.e. being able to press a stop button while the script is running) This is also working perfectly.

Now the problem: I just cannot communicate with the GUI while the script is running or at least I have no idea how. I'm trying to write output in a text window by passing "self" (the gui) to the script and in the script I try to do things like "self.outputWindow.WriteText('the script is doing bla 1 of bla 10')"

I even figured out why this won't work: self (the gui object) is not pickable and that's mandatory for multiprocessing, but I don't know how else I should do it.

回答1:

You can use my tutorial on wxPython and threads, although I'm not sure if Python spreads those threads evenly to all the cores. I suspect it doesn't.

Fortunately, there are examples of using the multiprocessing module with wxPython. See the following links:

  • http://wiki.wxpython.org/MultiProcessing
  • wxPython non-blocking GUI threading AND multiprocessing?
  • wxpython GUI and multiprocessing - how to send data back from the long running process
  • http://www.blog.pythonlibrary.org/2012/08/03/python-concurrency-porting-from-a-queue-to-multiprocessing/

I hope those are helpful!