PyQT和线程 [英] PyQT and threads

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

问题描述

我正在开发一个使用多个线程从各种网络设备收集数据的应用程序.我正在使用PyQT在GUI上显示收集的数据.我在我的应用程序(而不是QThread)中使用常规的python线程(来自线程,线程).为了在不同线程上更新GUI,我使用了一个锁(thread.allocate_lock()).因此,无论何时发生GUI更新,我都会带锁调用更新GUI.对此有任何担忧吗?

I am developing an application that uses multiple threads to gather data from a variety of network devices. I'm using PyQT to display the collected data on a GUI. I am using regular python threads (from thread, threading) in my app (instead of QThread). In order to update the GUI on the different threads, I use a lock (thread.allocate_lock()). So, anytime a GUI update will happen, I call with lock, update GUI. Any concerns about this?

推荐答案

我敢肯定,即使您尝试将代码锁定在自己的代码中,从Qt中的不同线程更新GUI也是危险的.一方面,Qt可能正在主线程上执行其自己的事件处理,并且它不会获取您的锁来保护它可能会修改的对象. 在此页面上在Qt文档中,QWidget是明确提到了不可重入或线程安全的问题.

I'm pretty sure that updating the GUI from different threads is dangerous in Qt, even if you try to lock things in your own code. For one thing, Qt might be doing its own event processing on the main thread, and it will not acquire your lock to protect objects that it might modify. On this page in the Qt docs, the fact that QWidget is not reentrant or thread-safe is explicitly mentioned.

我建议您将收集的数据或其处理后的版本发布回主线程.使用排队的信号/插槽连接,或使用自定义的QEventQApplication::postEvent来执行此操作.在jkerian提到的上一个问题中,它说如果要使事件发布正常工作,则必须使用QThread而不是python的线程.

I recommend that you post the collected data, or a processed version of it, back to the main thread. Use a queued signal/slot connection, or a custom QEvent and QApplication::postEvent to do this. In the previous question that jkerian mentions, it says that you'll have to use QThread instead of python's threads if you want event posting to work correctly.

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

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