PyQt 应用程序中的线程:使用 Qt 线程还是 Python 线程? [英] Threading in a PyQt application: Use Qt threads or Python threads?

查看:27
本文介绍了PyQt 应用程序中的线程:使用 Qt 线程还是 Python 线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 GUI 应用程序,它通过网络连接定期检索数据.由于此检索需要一段时间,这会导致 UI 在检索过程中无响应(无法拆分为更小的部分).这就是为什么我想将网络连接外包给一个单独的工作线程.

I'm writing a GUI application that regularly retrieves data through a web connection. Since this retrieval takes a while, this causes the UI to be unresponsive during the retrieval process (it cannot be split into smaller parts). This is why I'd like to outsource the web connection to a separate worker thread.

[是的,我知道,现在我有 两个问题.]

[Yes, I know, now I have two problems.]

无论如何,该应用程序使用 PyQt4,所以我想知道更好的选择是:使用 Qt 的线程还是使用 Python threading 模块?每个的优点/缺点是什么?或者你有什么完全不同的建议?

Anyway, the application uses PyQt4, so I'd like to know what the better choice is: Use Qt's threads or use the Python threading module? What are advantages / disadvantages of each? Or do you have a totally different suggestion?

编辑(赏金):虽然在我的特殊情况下的解决方案可能会使用像 Jeff OberLukáš Lalinský 建议(所以基本上将并发问题留给网络实现),我仍然希望对一般问题进行更深入的回答:

Edit (re bounty): While the solution in my particular case will probably be using a non-blocking network request like Jeff Ober and Lukáš Lalinský suggested (so basically leaving the concurrency problems to the networking implementation), I'd still like a more in-depth answer to the general question:

使用 PyQt4 的(即 Qt 的)线程而不是本机 Python 线程(来自 threading 模块)的优点和缺点是什么?

What are advantages and disadvantages of using PyQt4's (i.e. Qt's) threads over native Python threads (from the threading module)?

编辑 2:感谢大家的回答.尽管没有 100% 的一致,但似乎普遍认为答案是使用 Qt",因为这样做的优点是与库的其余部分集成,同时不会造成真正的缺点.

Edit 2: Thanks all for you answers. Although there's no 100% agreement, there seems to be widespread consensus that the answer is "use Qt", since the advantage of that is integration with the rest of the library, while causing no real disadvantages.

对于希望在两种线程实现之间进行选择的任何人,我强烈建议他们阅读此处提供的所有答案,包括 abbot 链接到.

For anyone looking to choose between the two threading implementations, I highly recommend they read all the answers provided here, including the PyQt mailing list thread that abbot links to.

我为赏金考虑了几个答案;最后我选择了方丈作为非常相关的外部参考;然而,这是一个危险的决定.

There were several answers I considered for the bounty; in the end I chose abbot's for the very relevant external reference; it was, however, a close call.

再次感谢.

推荐答案

这是 讨论 不久前在 PyQt 邮件列表中.引用 Giovanni Bajo 关于该主题的评论:

This was discussed not too long ago in PyQt mailing list. Quoting Giovanni Bajo's comments on the subject:

大体上是一样的.主要区别在于 QThreads 更好与 Qt 集成(异步信号/插槽、事件循环等).此外,您不能从 Python 线程使用 Qt(例如,您不能通过QApplication.postEvent向主线程post事件):你需要一个 QThread 才能工作.

It's mostly the same. The main difference is that QThreads are better integrated with Qt (asynchrnous signals/slots, event loop, etc.). Also, you can't use Qt from a Python thread (you can't for instance post event to the main thread through QApplication.postEvent): you need a QThread for that to work.

如果您要以某种方式与 Qt 交互,一般的经验法则可能是使用 QThreads,否则使用 Python 线程.

A general rule of thumb might be to use QThreads if you're going to interact somehow with Qt, and use Python threads otherwise.

PyQt 的作者之前对这个主题的一些评论:它们都是围绕相同本机线程实现的包装器".并且两个实现都以相同的方式使用 GIL.

And some earlier comment on this subject from PyQt's author: "they are both wrappers around the same native thread implementations". And both implementations use GIL in the same way.

这篇关于PyQt 应用程序中的线程:使用 Qt 线程还是 Python 线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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