Python & 中的线程错误PyQt [英] Thread error in Python & PyQt

查看:68
本文介绍了Python & 中的线程错误PyQt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当函数 setModel 在并行线程中执行时(我试过 threading.Timer 或 threading.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?

推荐答案

Qt(和其他丰富的框架)的多线程使用确实是生活中的一个事实,这是一项微妙而困难的工作,需要明确的关注和照顾——请参阅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).

如果可能的话,我会建议我一直建议的 Python 中最合理的线程架构:让每个子系统都由一个专用线程拥有和使用;通过 Queue.Queue 的实例在线程之间进行通信,即通过消息传递.这种方法可能有点限制,但它为专门识别和精心设计的异常(基于线程池、偶尔产生的新线程、锁、条件变量和其他此类挑剔的东西;-)提供了良好的基础.在后一类中,我还将通过 排队连接.

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.

这篇关于Python & 中的线程错误PyQt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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