Python多重处理和wxPython协同工作 [英] Python multiprocessing and wxPython working together

查看:136
本文介绍了Python多重处理和wxPython协同工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

我编写了一个脚本,该脚本可以同时运行四个进程.通过命令行使用它时,就像魅力一样. 然后,我决定使用wxPython编写GUI,然后我很快就弄清楚了GUI和脚本需要在不同的进程中运行,以便两者在做某事时都保持可用. (即,在脚本运行时能够按下停止按钮)这也可以正常工作.

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.

现在的问题是: 脚本运行时,我只是无法与GUI通信,或者至少我不知道该怎么做.我试图通过将"self"(gui)传递给脚本来在文本窗口中写入输出,并且在脚本中我尝试执行诸如"self.outputWindow.WriteText('脚本正在执行bla 10 bla 1"的操作')"

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')"

我什至弄清楚了为什么这行不通:self(gui对象)无法选择,这对于多处理是必不可少的,但是我不知道该怎么办.

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.

推荐答案

您可以使用我的教程,尽管我不确定Python是否将这些线程均匀地分布到所有内核.我怀疑不是.

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.

幸运的是,有一些将多处理模块与wxPython一起使用的示例.请参阅以下链接:

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!

这篇关于Python多重处理和wxPython协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆