3

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

opensw
  • 427
  • 5
  • 15
  • 2
    Are you on windows? If so are you using the `if __name__ == '__main__'` guard to spawn the UI? Anyway, I believe what you want cannot be achieved, because the `fork` copies all the threads, also the UI one(thus spawning more UIs). You should probably put the parallel computation in an "external" processes and call that from the UI. – Bakuriu Mar 20 '13 at 10:39
  • Yes, I am on windows and I don't know anything about if __name__ == '__main__' guard to spawn the UI xD because I am new in python xD. OK, I will try to do the computation with external processes, thx :) – opensw Mar 20 '13 at 10:51

3 Answers3

4

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()
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
1

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.

  • Like @Bakuriu, are you suggesting me to use external processes for computation and call them from main GUI? Thx :) – opensw Mar 20 '13 at 10:55
  • 1
    You can do that, yes. The `if __name__ == '__main__'` trick basically says "if you're calling this file directly (not from a module), run the following code." It's common to have some sort of main.py that runs your GUI, leaving imports to do your dirty work. You don't _have_ to separate your code into multiple files though -- just separate your code as such that you can call your computation functions without invoking pyqt. –  Mar 20 '13 at 10:59
1

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)
    ...
ChrisPHL
  • 11
  • 3