Python:Pyqt应用程序中的多处理 [英] Python: multiprocessing in pyqt application
问题描述
我的计算机中装有i7 CPU,并且为了提高pyqt应用程序的时间计算性能,我尝试使用多处理模块;当我在pyqt应用程序中执行以下操作时:
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))
然后,该应用程序生成8个pyqt GUI,它们是第一个主窗口的克隆(总共我有9个pyqt GUI,这当然是错误的,我要做的是并行计算,而没有克隆主GUI xD ).
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).
我也尝试了joblib库( http://pythonhosted.org/joblib/),但是问题是一样.
I tried joblib library too (http://pythonhosted.org/joblib/) but the problem is the same.
有没有一种方法可以在带有多处理或joblib模块的pyqt应用程序中进行并行计算?
Is there a way to do the parallel computation in a pyqt application with multiprocessing or joblib module?
感谢您的帮助
推荐答案
如果您使用的是Windows,则多处理将启动导入主模块的新进程.确保将GUI创建代码放在if __name__ == '__main__':
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__':
更好的是,为避免在子流程中不必要地导入PyQt的开销,请创建一个简单的新主模块,如下所示:
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()
这篇关于Python:Pyqt应用程序中的多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!