如何在PyQt4进程中使用多核python? [英] How to use multicore python with PyQt4 process?

查看:100
本文介绍了如何在PyQt4进程中使用多核python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在pyqt4中编写一个必须读取并解析许多xml文件的应用程序.完成单线程处理需要花一些时间才能完成所有解析,并制作与该传入xml相对应的数千个python对象.我已经分析了代码,据我所知它是计算的,而不是I/O的.

I am writing an app in pyqt4 that has to read in and parse a lot of xml files. Done single-threaded it takes a while to do all that parsing and make the thousands of python objects corresponding to that incoming xml. I have profiled the code and as far as I can tell it's compute, not I/O, bound.

我想使用工人农场模型(python中的Process.Pool)将应用程序转换为多核模型以分散负载.

I would like to convert the app to a multi-core model to spread the load around, using a worker-farm model (?Process.Pool in python).

但是,我也希望能够通过信号通知工作人员来更新gui.

However, I would also like to be able to Signal progress from the workers to update the gui.

从我到目前为止所读的内容看来,QThread不具有多核功能(因为它在一个核上循环运行),但是我需要QThread来执行Signal,因此从本质上讲我不能做到这一点.

It seems to me from what I have read so far that QThread is not multicore capable (because it round-robins on one core) but I need QThread to do Signal, and so essentially I can't do that.

我也许可以安排不需要工人发出信号,而只需要从农民那里发出信号,这可能意味着我可以继续下去,但是随后我想知道:我可以将python对象列表从一个Process返回到另一个?

I might be able to arrange not to need to Signal from a worker, only from the farmer, which might then mean I can carry on, but then I wonder: can I return a list of python objects from one Process to another?

推荐答案

  • 产生一个QThread.
  • QThread可以将任务种植到多处理池中.你可能会 使用 pool.apply_async()其中有一个回调参数.
  • 回调参数允许您指定一个函数,该函数是 在目标函数完成时调用.
  • 该回调在QThread中运行,并被发送给 目标函数是其唯一的参数.
  • 每次运行回调函数时,都可以将GUI更新为 指示进度.
    • Spawn a QThread.
    • The QThread can farm out tasks to the multiprocessing Pool. You might use pool.apply_async() which has a callback parameter.
    • The callback parameter allows you to specify a function which is called when the target function completes.
    • The callback runs in the QThread, and is sent the return value of the target function as its one and only argument.
    • Each time the callback function runs, you can update the GUI to indicate the progress.
    • 这篇关于如何在PyQt4进程中使用多核python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆