How to make this script loop every hour

后端 未结 3 796
暖寄归人
暖寄归人 2021-01-28 12:11

I\'m creating a bot that posts every 60 minutes covid numbers, I have it finished but idk how to make it repeat. Any idea? It\'s a little project that I have in mind and it\'s t

相关标签:
3条回答
  • 2021-01-28 12:42

    You have to use a timer like this one below. (The function refresh refers to itself every hour)

    from threading import Timer
    
    def refresh(delay_repeat=3600): # 3600 sec for one hour
        # Your refresh script here
        # ....
        # timer
        Timer(delay_repeat, refresh, (delay_repeat, )).start()
    

    Have a look at my runnable refresh graph here to have an idea : How can I dynamically update my matplotlib figure as the data file changes?

    0 讨论(0)
  • 2021-01-28 12:57

    You might want to use a scheduler, there is already a built-in one in Python (sched), read more about it here: https://docs.python.org/3/library/sched.html

    0 讨论(0)
  • 2021-01-28 12:58

    You can simply add this statement at the end of the code

    sleep(3600)
    

    If you want it to run endlessly, you can do it like this:

    while True:
        insert your main code here
        sleep(3600)
    
    0 讨论(0)
提交回复
热议问题