多处理问题 [pyqt, py2exe] [英] multiprocessing problem [pyqt, py2exe]

查看:57
本文介绍了多处理问题 [pyqt, py2exe]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt4 编写 GUI 程序.我的主窗口中有一个按钮并通过单击此按钮.我希望启动一个后台进程这是派生类的实例来自加工过程.

I am writing a GUI program using PyQt4. There is a button in my main window and by clicking this button. I hope to launch a background process which is an instance of a class derived from processing.Process.

class BackgroundTask(processing.Process):
    def __init__(self, input):
        processing.Process.__init__(self)
        ...

    def run(self):
        ...

(注意我使用的是 Python2.5 端口获得的python-multiprocessing从http://code.google.com/p/python-multiprocessing/这就是它正在处理的原因.Process而不是 multiprocessing.Process.我想这应该没什么区别.我说得对吗?)

(Note that I am using the Python2.5 port of the python-multiprocessing obtained from http://code.google.com/p/python-multiprocessing/ that is why it is processing.Process instead of multiprocessing.Process. I guess this should not make a difference. Am I right?)

连接按钮点击信号的代码就像

The code connected to the button click signal is something simply like

 processing.freezeSupport()
 task = BackgroundTask(input)
 task.start()

该程序在 python 解释器下按预期工作,即如果它是从命令行python myapp.py"启动的.

The program works as expected under the python intepreter, i.e. if it is started from the command line "python myapp.py".

但是,我用py2exe打包程序后,每次当我点击那个按钮时,启动后台任务,副本弹出主窗口.我不确定这种行为的原因是什么.我猜它与以下说明有关在http://docs.python.org/library/multiprocessing.html#multiprocessing-programming

However, after I package the program using py2exe, everytime when I click that button, instead of starting the background task, a copy of the main window pops up. I am not sure what is the reason of this behavior. I guess it is related to the following note addressed at http://docs.python.org/library/multiprocessing.html#multiprocessing-programming

"这个包中的功能要求 ma​​in 方法可以被子进程导入.这在编程指南中有介绍,但是这里值得指出.这意味着一些示例,例如多处理.Pool 示例在交互式解释器中不起作用"

"Functionality within this package requires that the main method be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter "

如果 name == "ma​​in" 我唯一的地方是在主模块中就像在典型的 pyqt 程序中一样

The only place I have if name == "main" is in the main module as in a typical pyqt program

if __name__ == "__main__":
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
    w = MainWindow()
    w.show()
    a.exec_()

有关如何解决此问题的任何解决方案?谢谢!

Any solutions on how to fix this problem? Thanks!

推荐答案

我认为您的实际问题与此有关:

I think your actual problem has to do with this:

该程序在 python 解释器下按预期工作,即如果它是从命令行python myapp.py"启动的.

The program works as expected under the python intepreter, i.e. if it is started from the command line "python myapp.py".

但是,我用py2exe打包程序后,每次点击那个按钮,>不是启动后台任务,而是弹出主窗口的副本.

However, after I package the program using py2exe, every time when I click that button, > instead of starting the background task, a copy of the main window pops up.

您需要添加对 freeze_support() 函数的特殊调用,以使多处理模块与frozen"一起工作.可执行文件(例如,那些用 py2exe 制作的):

You need to add a special call to the freeze_support() function to make the multiprocessing module work with "frozen" executables (eg, those made with py2exe):

if __name__ == "__main__":
    # add freeze support
    processing.freeze_support()
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
    w = MainWindow()
    w.show()
    a.exec_()

参考:http://docs.python.org/library/multiprocessing.html#multiprocessing.freeze_support

这篇关于多处理问题 [pyqt, py2exe]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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