[英]How do I call a function every 24 hours in Python? I'm currently using Threading to run a Flask server and the function at the same time
Right now, I'm trying to make a graphing website that tracks the progress (XP) of Discord users with a bot called MEE6, here is my repl .现在,我正在尝试使用名为 MEE6 的机器人来制作一个图形网站来跟踪 Discord 用户的进度(XP),这是我的repl 。 Right now, I'm using Threading to create two separate threads - one for a web server, and one containing a while loop with the function inside:现在,我正在使用 Threading 创建两个单独的线程 - 一个用于 Web 服务器,另一个包含带有内部函数的 while 循环:
def func():
while True:
backend.get_details()
time.sleep(86400)
This should make the function run every 24 hours, but as evidenced by the time stamps in the database:这应该使函数每 24 小时运行一次,但正如数据库中的时间戳所证明的那样:
"05-November-2021 00:02:58": 2106855,
"05-November-2021 00:52:48": 2106855,
"05-November-2021 01:23:21": 2106855,
"05-November-2021 03:48:13": 2106874,
"05-November-2021 07:13:40": 2106874
It is not.它不是。 How can I fix this?我怎样才能解决这个问题?
Here is my threading code:这是我的线程代码:
def keep_alive():
server = Thread(target=run)
data = Thread(target=func)
server.start()
data.start()
def run():
app.run(host="0.0.0.0", port=8000)
if __name__ == '__main__':
# s.run()
# os.system('cls')
keep_alive()
# print('i')
Have you tried fixing it using the schedule
package?您是否尝试使用schedule
包修复它? For an example see this post:有关示例,请参阅此帖子:
Python script to do something at the same time every day 每天在同一时间做某事的 Python 脚本
For running a scheduler in the background (ie while running an app) see this excellent post:要在后台运行调度程序(即在运行应用程序时),请参阅这篇出色的文章:
How to schedule a function to run every hour on Flask? 如何安排一个函数在 Flask 上每小时运行一次?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.