PyQt4 中的线程
我正在制作一个 PyQt4 应用程序,它调用 Imagemagick 和 ffmpeg,但完成任务需要太多时间,我想知道是否有办法实现线程,该应用程序将在多核计算机中运行,以及一些方法我见过效率不够高的。提前致谢
Im making a PyQt4 app which calls Imagemagick and ffmpeg, but it takes too much to complete the tasks and im wondering if there is a way to implement threads to this, the app is going to run in a multicore machine, and some of the methods I have seen are not efficient enough. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是肯定和否定 - 您可以使用 QThread 进行线程处理,但您仍然会受到 GIL 的约束,因此您的线程将串行运行。
您可以尝试使用多处理在自己的进程中创建一个工作类并向其发送工作(或让它从队列中窃取工作),但是这可能会在复制对象和在进程之间发送对象时引入其自身的性能损失
...重新阅读你的问题,看起来 Imagemagick 和 ffmpeg 是外部可执行文件,在这种情况下,当你等待进程执行时 GIL 被释放。我能问一下你是如何运行这些的吗?我倾向于发现最好创建一个工作队列和一个事件循环。每次事件循环时,您都会检查正在运行的进程是否已完成,然后获取其输出。对于这个 subprocess.Popen 比 os.command 更有用。
如果您使用 QTimer,您可以利用 QApplication 的事件循环,这还有一个额外的好处,即允许您的 GUI(假设您有一个)在滴答之间刷新。
The answer is yes and no - you can use QThread for threading but you'll still be subject to the GIL, so your threads will run in serial.
You could try using multiprocessing to create a worker class in its own process and send work to it (or have it steal work from a queue), however this could introduce its own performance penalties in copying objects and sending objects between processes...
Having re-read your question, it looks like Imagemagick and ffmpeg are external executables, in which case the GIL is released while you wait for the process to execute. Can I ask how you run these though? I tend to find that it is better to create a work queue and an event loop. Each time round the event loop you check whether your running process(es) have finished and then get their output. For this subprocess.Popen is more useful than os.command.
If you use a QTimer you can utilise your QApplication's event loop, which also has the added benefit of allowing your GUI (assuming you have one) to be refreshed between ticks.