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

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

问题描述

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

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/ 这就是为什么它正在处理. 而不是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

此程序包中的功能要求孩子可以导入 main 方法.这在编程指南中已涉及到,但是在这里值得指出.这意味着一些示例,例如多处理池示例在交互式解释器中不起作用 "

"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 ==" main "在主模块中,则只有我一个位置 就像在典型的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()函数添加一个特殊调用,以使多处理模块可以使用冻结"的可执行文件(例如,使用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天全站免登陆