简体   繁体   English

如何在 Python 中安排周期性任务?

[英]How to schedule a periodic task in Python?

How can I schedule a periodic task in python without blocking? python - 如何在不阻塞的情况下在python中安排定期任务?

Here is a simple situation.这是一个简单的情况。 Assume this ticket variable becomes invalid after 2 hours.假设这个工单变量在 2 小时后失效。 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?如何在不阻塞的情况下每两小时重置为 1?

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.一种方法可能是启动一个线程并睡眠 2 小时,然后重置变量,但我想知道是否有更好的选择。

Note: Everything runs in a docker.注意:一切都在 docker 中运行。

This looks like a use case for cron , a simple utility found on all linux machines to schedule periodic tasks.这看起来像是cron一个用例,这是一个在所有 Linux 机器上都可以找到的简单实用程序,用于安排定期任务。 A simple cron task could be created like:可以创建一个简单的cron任务,例如:

$ crontab -e

Within the cron, make an entry在 cron 中,输入

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如果您在脚本中打印某些内容,请使 cron 条目重定向其输出,例如

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:对于在 Docker 中运行它的问题,你可以通过这个线程 ->https://forums.docker.com/t/cronjobs-in-docker-container/2618/5它说:

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.大多数 Docker 容器仅运行您指定的二进制文件(在本例中为 /bin/bash),因此没有运行 cron 守护程序来触发这些作业。

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 ...有多种方法可以解决这个问题 - 对于小的临时事物,您可以将 cron 条目添加到主机的 crontab 中,并使用 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.您可以使用启动 cron 的 shell 脚本替换容器入口点,或者如果您需要多个服务,请使用诸如 supervisord 之类的东西来管理它们。

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.如果您全力以赴,您可以制作一个 cron 容器,然后您可以使用它来使用 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 .另一种方法是你已经拥有的 - 睡眠 2 小时,或保持time_last_read时间戳,继续评估自上次读取以来是否已经 2 小时( time_now - time_last_read >= 2_HOURS ),然后将值重新读入内存一旦条件为 True 并重置time_last_read

Put it in a function with time.sleep(7200000) .将其放入带有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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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