使用python在SQLite数据库中重复写入数据
我想将 SQLite 用于我的 GUI Python 应用程序,但我必须每 500 MS 更新一次数据库,而不会影响程序的性能。
我正在使用PyQt4,所以我考虑使用QThread,但它似乎很难处理,所以我想知道在真正尝试理解它之前这是否是最好的方法。
我的问题是: QThread 是最好的方法还是还有其他方法?
I want to use SQLite for my GUI Python application but I have to update database every 500 MS without effecting the performance of my program.
I'm using PyQt4,So I thought about using QThread but it seems difficult to deal with, so I wondered if it was the best way before really trying to understand it.
My Question is: is QThread the best way or there are other ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据Python实现依赖于 GIL,即使使用线程或计时器,您也无法在不影响程序全局性能的情况下在程序中执行某些操作(可能成本高昂)。
我建议您查看 multiprocessing 模块来解决此限制。使用此模块,您将不再使用线程(受 GIL 影响),而是使用进程(不受 GIL 影响)。
也许您可以创建一个子进程,当主进程继续其工作时,该子进程将配备一个计时器,每 500 毫秒进行更新。
然后,您将让系统完成平衡程序的工作,并且在响应能力方面可能会更好(尤其是在多核环境中)
According to the fact that python implementation rely on the GIL, even with using threads or timer you won't be able to do something (potentially costly) in your program without effecting the global performance of the program.
I will suggest you to have a look to multiprocessing module to get around of this limitation. Using this module, you will no more use threads (that are affected by the GIL), but processes (not affected by GIL).
Maybe you could create a subprocess that will arm a timer to make the update every 500ms when the main process will continue his job.
Then, you will let the system do the job of balancing the programs and it may be better in term of responsiveness (especially in a multi core environment)