Python中的线程错误&;PyQt
我注意到,当函数在并行线程中执行时(我尝试了threading.Timer或threading.thread),我得到以下结果:Python中的线程错误&;PyQt,python,multithreading,pyqt,Python,Multithreading,Pyqt,我注意到,当函数在并行线程中执行时(我尝试了threading.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: 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
有什么办法可以解决这个问题吗?看起来你已经被Qt限制难住了。如果需要对象跨线程通信,请尝试使用信号或事件
或者问问Qt的人。这似乎并不特定于PyQt。事实上,多线程使用Qt(和其他富框架)是一项微妙而困难的工作,需要明确的关注和照顾——请参阅本主题的精彩报道(对于一般有线程经验的读者,对于那些还没有线程经验的读者,建议阅读)
如果可能的话,我会推荐我一直建议的Python线程最可靠的体系结构:让每个子系统由一个专用线程拥有和使用;线程之间通过
队列的实例进行通信。队列
,即通过消息传递。这种方法可能有点限制,但它提供了一个很好的基础,具体地识别和精心设计异常(基于线程池,偶尔产生新线程,锁,条件变量,和其他这样的棘手的事情;)。在后一个类别中,我还将对Qt特定的东西进行分类,例如跨线程信号/插槽通信通过。对于一个非常有效的问题来说,这是一个非常糟糕的标题。为什么不把它称为“如何在Python和PyQt中使用多线程”呢?