Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

python - multiprocessing problem [pyqt, py2exe]

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):
        ...

(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()

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

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

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think your actual problem has to do with this:

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

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.

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_()

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...