Search code examples
pythonschedule

Using Schedule to run jobs at certain times


I am trying to schedule a few jobs and thanks to stackoverflow I was able to find Schedule (https://pypi.python.org/pypi/schedule).

Unfortunately this isn't documented very well. I have a list of jobs I am trying to run, each with a certain time in a single day that it is supposed to be run.

for i in jobArray:
    # compareTimes returns a boolean that indicates if it's time to run it, assume it works.
    if compareTimes(i.time):
        # I want to schedule this job for a specific time, i.time
        schedule.every().day.do(job)
while 1:
    schedule.run_pending()
    time.sleep(60)

This is what I came up with. Obviously this is wrong (it gets stuck in the while loop). There is no documentation for .run_pending() so I don't know how to fix it to get it to work. Please assist.

Thanks.


Solution

  • This is what I came up with. Obviously this is wrong (it gets stuck in the while loop). There is no documentation for .run_pending() so I don't know how to fix it to get it to work. Please assist.

    That appears to be the correct behaviour. The scheduler is checking for pending jobs every 60 seconds, and (I assume) is running them as they come in. It is supposed to run forever, because are you scheduling jobs perpetually with schedule.every().day.do(job). Your problem is that you need to run this program in the background.

    If you are interested in something that is suitable for a production environment, I suggest taking a look at either Celery (which my very well be overkill) or Python RQ. These are well documented, distributed, and can run in the background out of the box.