Python 中的线程错误pyqt
我注意到当函数 setModel 在并行线程中执行时(我尝试过线程.Timer 或 threading.thread),我得到这个:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QHeaderView(0x1c93ed0), parent's thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTreeView(0xc65060), parent's thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
有什么办法解决这个问题吗?
I noticed that when the function setModel is executed in parallel thread (I tried threading.Timer or threading.thread), I get this:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QHeaderView(0x1c93ed0), parent's thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTreeView(0xc65060), parent's thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
Is there any way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实是,Qt(和其他丰富的框架)的多线程使用是一项微妙而困难的工作,需要明确的关注和照顾——请参阅Qt 文档 对该主题进行了精彩的报道(对于一般而言具有线程经验的读者,对于那些还没有线程经验的读者,建议阅读)。
如果可以的话,我会建议我一直建议的Python中最健全的线程架构:让每个子系统由一个专用线程拥有和使用;通过 Queue.Queue 实例在线程之间进行通信,即通过消息传递。这种方法可能有点限制,但它为专门识别和精心构建的异常提供了良好的基础(基于线程池、偶尔生成的新线程、锁、条件变量和其他此类挑剔的东西;-)。在后一类中,我还将对 Qt 特定的事物进行分类,例如通过 排队连接。
It is indeed a fact of life that multithreaded use of Qt (and other rich frameworks) is a delicate and difficult job, requiring explicit attention and care -- see Qt's docs for an excellent coverage of the subject (for readers experienced in threading in general, with suggested readings for those who yet aren't).
If you possibly can, I would suggest what I always suggest as the soundest architecture for threading in Python: let each subsystem be owned and used by a single dedicated thread; communicate among threads via instances of
Queue.Queue
, i.e., by message passing. This approach can be a bit restrictive, but it provides a good foundation on which specifically identified and carefully architected exceptions (based on thread pools, occasional new threads being spawned, locks, condition variables, and other such finicky things;-). In the latter category I would also classify Qt-specific things such as cross-thread signal/slot communication via queued connections.看起来你已经被 Qt 的限制难住了。如果需要对象跨线程通信,请尝试使用信号或事件。
或者向 Qt 人员询问这一点。它似乎不是 PyQt 特有的。
Looks like you've stumped on a Qt limitation there. Try using signals or events if you need objects to communicate across threads.
Or ask the Qt folk about this. It doesn't seem specific to PyQt.