opensw
opensw

Reputation: 425

Python: multiprocessing in pyqt application

I have an i7 CPU in my computer and to improve the performance in time computation for my pyqt application I am trying to use the multiprocessing module; when I do in a pyqt application something like this:

import multiprocessing as multiprocessing

def foo(ii):
    print ii

pool = multiprocessing.Pool(8)
pool.map(foo, range(10))

then the application generates 8 pyqt GUIs that are the clones of the first main window (in total I have 9 pyqt GUI that it is of course wrong, what I want to do is the parallel computation and no clone the main GUI xD).

I tried joblib library too (http://pythonhosted.org/joblib/) but the problem is the same.

Is there a way to do the parallel computation in a pyqt application with multiprocessing or joblib module?

Thanks for any help

Upvotes: 3

Views: 7060

Answers (3)

ChrisPHL
ChrisPHL

Reputation: 11

I came accross here after running into the "multiple GUIs on multioprocessing.Pool()" problem. After a while I found the solution here respectively here:

from multiprocessing import freeze_support

if __name__ == '__main__':
    freeze_support()

    a = QApplication(sys.argv)
    ...

Upvotes: 1

Janne Karila
Janne Karila

Reputation: 25197

If you are on Windows, multiprocessing will launch new processes that import your main module. Be sure to protect the GUI creation code by placing it under if __name__ == '__main__':

Better yet, to avoid the overhead of importing PyQt unnecessarily in the subprocesses, create a simple new main module like this:

if __name__ == '__main__':
    import old_main_module
    old_main_module.main()

Upvotes: 4

user559633
user559633

Reputation:

Do you want to spawn multiple processes for pyqt or do you want to add additional processes for the 'logic' of your application?

More to the point: don't multiprocess the pyqt container - if you want parallelism, spawn processes on the logic of your application and return the result to your view layer.

Upvotes: 1

Related Questions