定时任务periodic tasks:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html
介绍
celery beat is a scheduler; It kicks off tasks at regular intervals, that are then executed by available worker nodes in the cluster.
celery beat是一个调度器,它在一定间隔后,通过集群中的可用的worker执行任务。
By default the entries are taken from the beat_schedule
setting, but custom stores can also be used, like storing the entries in a SQL database.
入口采用默认的beat调度器设置,也可以用自定义的,如储存在数据库中的入口。
You have to ensure only a single scheduler is running for a schedule at a time, otherwise you’d end up with duplicate tasks. Using a centralized approach means the schedule doesn’t have to be synchronized, and the service can operate without using locks.
一个时间表每次只能有一个调度器运行,否则重复任务终止。用集中的方法意味着时间表不必同步,服务端可以不用锁而操作。
时区
The periodic task schedules uses the UTC time zone by default, but you can change the time zone used using the timezone
setting.
定时任务时间表默认是UTC时区,可以自定义。
timezone = 'Asia/Shanghai'
This setting must be added to your app, either by configuration it directly using (app.conf.timezone = 'Europe/London'
), or by adding it to your configuration module if you have set one up using app.config_from_object
. See Configuration for more information about configuration options.
这个设置必须添加到app(项目settings中?)中,也可以直接配置 app.conf.timezone = 'Asia/Shanghai',也可以把它添加到配置模块中 app.config_from_object
The default scheduler (storing the schedule in the celerybeat-schedule
file) will automatically detect that the time zone has changed, and so will reset the schedule itself, but other schedulers may not be so smart (e.g., the Django database scheduler, see below) and in that case you’ll have to reset the schedule manually.
Entries 入口
To call a task periodically you have to add an entry to the beat schedule list.
将一个入口添加到beat时间表列表后才能定期的调用任务
from celery import Celery from celery.schedules import crontab app = Celery() @app.on_after_configure.connect #脚本启动就执行该函数 def setup_periodic_tasks(sender, **kwargs): # Calls test('hello') every 10 seconds. sender.add_periodic_task(10.0, test.s('hello'), name='add every 10') # Calls test('world') every 30 seconds sender.add_periodic_task(30.0, test.s('world'), expires=10) # Executes every Monday morning at 7:30 a.m. sender.add_periodic_task( crontab(hour=7, minute=30, day_of_week=1), test.s('Happy Mondays!'), ) @app.task def test(arg): print(arg)
Setting these up from within the on_after_configure
handler means that we’ll not evaluate the app at module level when using test.s()
.
The add_periodic_task()
function will add the entry to the beat_schedule
setting behind the scenes, and the same setting can also be used to set up periodic tasks manually:
Example: Run the tasks.add task every 30 seconds.
app.conf.beat_schedule = { 'add-every-30-seconds': { 'task': 'tasks.add', 'schedule': 30.0, 'args': (16, 16) }, } app.conf.timezone = 'UTC'
Note If you’re wondering where these settings should go then please see Configuration.
You can either set these options on your app directly or you can keep a separate module for configuration. If you want to use a single item tuple for args, don’t forget that the constructor is a comma, and not a pair of parentheses.
Using a timedelta
for the schedule means the task will be sent in 30 second intervals (the first task will be sent 30 seconds after celery beat starts, and then every 30 seconds after the last run).
A Crontab like schedule also exists, see the section on Crontab schedules.
Like with cron, the tasks may overlap if the first task doesn’t complete before the next. If that’s a concern you should use a locking strategy to ensure only one instance can run at a time (see for example Ensuring a task is only executed one at a time).
Avaliable fields
task The name of the task to execute. schedule The frequency of execution. This can be the number of seconds as an integer, a timedelta, or a crontab. You can also define your own custom schedule types, by extending the interface of schedule. args Positional arguments (list or tuple). kwargs Keyword arguments (dict). options Execution options (dict). This can be any argument supported by apply_async() – exchange, routing_key, expires, and so on. relative If relative is true timedelta schedules are scheduled “by the clock.” This means the frequency is rounded to the nearest second, minute, hour or day depending on the period of the timedelta. By default relative is false, the frequency isn’t rounded and will be relative to the time when celery beat was started.
Crontab Scheduls
If you want more control over when the task is executed, for example, a particular time of day or day of the week, you can use the crontab
schedule type:
from celery.schedules import crontab app.conf.beat_schedule = { # Executes every Monday morning at 7:30 a.m. 'add-every-monday-morning': { 'task': 'tasks.add', 'schedule': crontab(hour=7, minute=30, day_of_week=1), 'args': (16, 16), }, }
Start the Scheduler
To start the celery beat service:
$ celery -A proj beat