简体   繁体   English

如何在 Python 中每 24 小时调用一次函数? 我目前正在使用线程同时运行 Flask 服务器和函数

[英]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.

相关问题 Python - 线程 - 如何创建播放列表函数以使用线程在后台运行? - Python - threading - how do I create a playlist function to run in the background with threading? 在 24 小时内每“X”小时运行一次函数“X”秒 - Run Function Every "X" hours for "X" Seconds over 24 hours 在Python中,如何遍历此列表并将每个项目调用到同一函数? - In Python, how do I loop through this list and call every item to the same function? 如何在 flask 应用程序中每 12 小时运行一次 function? - How to to run a function every 12 hours inside a flask app? 如何使用 Jinja2 html 模板在 Flask 服务器上调用 python 函数? - How can I call a python function on the Flask server using a Jinja2 html template? 如何在 Flask 中使用 SQLAlchemy 调用数据库函数? - How do I call a database function using SQLAlchemy in Flask? Python线程-如何使用它一次运行多个任务? - Python Threading - How do i use it to run multiple tasks at time? 如何使用不同的参数对相同的 function 执行线程处理? - How do I implement threading to the same function but with different parameters? 我正在使用 Jaeger 跟踪 python 函数。 我是否必须手动在每个 function 中创建跨度? - I'm using Jaeger tracing python functions. Do i have to create spans in every function manually? 如何制作一个每 24 小时执行一次的函数而不用 time.sleep() 停止整个程序? - How to make a function that is done every 24 hours without stopping the whole program with time.sleep() for that time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM