Home > Mobile >  Repeat functions 500 per day
Repeat functions 500 per day

Time:06-02

How can I repeat a function 500 times per day? I have used time module and loop command. This is my example:

import schedule
import time

def job(t):
    print "I'm working...", t
    return

schedule.every().day.at("01:00").do(job,'It is 01:00')

while True:
    schedule.run_pending()
    time.sleep(60)

CodePudding user response:

This is a simple example so if you want to run this 500 times per day you need some math knowledge.

you need to change one day to min = 1440 min

we gonna divide 1440 by 500 times to get how much time we need to run this function 500 times in the day so the result is 2.88 min.

import time

while True:
    schedule.run_pending()
    time.sleep(2.88 * 60) # time.sleep(minutes * 60)

CodePudding user response:

I don't think this is the best way to do scheduled (periodic) tasks/jobs. Because your process is always alive and makes a process busy. I offer you to use celery beat to schedule your task very simple and efficient: https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html#:~:text=The default scheduler is the,manage periodic tasks at runtime.

  • Related