简体   繁体   中英

How to schedule a periodic task in Python?

How can I schedule a periodic task in python without blocking?

Here is a simple situation. Assume this ticket variable becomes invalid after 2 hours. So I need to fetch it from a serve.

ticket = 1 # It expires every 2 hours

def process_using_ticket(): # This function is called using a get request from flask server 
    print('hello', ticket) 

How can I reset to 1 every two hours without blocking?

One way could be to start a thread and sleep for 2 hours and then reset the variable but I am wondering if there are better alternatives.

Note: Everything runs in a docker.

This looks like a use case for cron , a simple utility found on all linux machines to schedule periodic tasks. A simple cron task could be created like:

$ crontab -e

Within the cron, make an entry

0 */2 * * *  /home/username/ticket_script.py

Make sure that your script has executable permissions. In case you are printing something in your script, make the cron entry to redirect its ouput like

0 */2 * * *  /home/username/ticket_script.py >> /home/username/ticket_script_runs.log

For gotchas on running this in Docker, you can go through this thread ->https://forums.docker.com/t/cronjobs-in-docker-container/2618/5 which says:

Most Docker containers only run the binary you specify (in this case /bin/bash), so there is no cron daemon running to trigger these jobs.

There are a number of ways you can deal with this - for small adhoc things, you can add the cron entry to your host's crontab, and trigger the job in the container using docker exec containername ...

You can replace the container entrypoint with a shell script that starts the cron, or if you have a need for several services, use something like supervisord to manage them.

And if you're going all in, you can make a cron container which you can then use to kick off tasks using docker exec.


The other approach is what you already have - go to sleep for 2 hours, or maintain a time_last_read timestamp, keep evaluating if its been 2 hours since your last read ( time_now - time_last_read >= 2_HOURS ), and re-read the value into memory once the condition is True and reset time_last_read .

Put it in a function with time.sleep(7200000) . Like so:

import time

ticket = 1

def main_loop():
    while True:
        time.sleep(7200000)
        ticket = 1

def process_using_ticket():
    print('hello', ticket) 
main_loop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM