How to get function to run at specyfic time using python and PyQt not using Cron

依然范特西╮ 提交于 2021-02-17 06:13:05

问题


I'm creating RSS app in PyQt and I'm trying to find a good way to program updates. I found this Executing periodic actions in Python but maybe there is Qt specific way to do this things.

I know update period for each feed so I want to run update at specific time(hh:mm).

Making 10 minute loop that will check current time and run a update if its grater than next predicted feed update seems missing the point of knowing specific time to run it.


回答1:


You should use QTimer in Qt applications. Usually you don't need to care about specific update time, as the goal is regular periodic check. So the most straightforward approarch is to create a timer for each feed and set the update interval of each timer (e.g. 10 minutes).

If you for some reason really want to make an update at specific time, you can use something like QDateTime::currentDateTime().msecsTo(targetTime) to calculate timer interval, use QTimer::setSingleShot to make the timer non-periodic and set another timer when the first one is expired.

It may be reasonable to do timer->setTimerType(Qt::VeryCoarseTimer) because you don't need much accuracy and Qt can optimize performance and power consuming in some cases.

Note that you generally cannot use Python's means to set up timers because Qt has its own event loop and won't allow other libraries to run something in the middle of it.



来源:https://stackoverflow.com/questions/28900532/how-to-get-function-to-run-at-specyfic-time-using-python-and-pyqt-not-using-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!