celery参考地址:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#starting-the-scheduler
安装依赖文件:
# 生成命令 pip freeze >> requirement.txt
# 安装命令 pip install -r requirement.txt
DateTime==4.3
amqp==2.4.2
billiard==3.6.0.0
celery==4.3.0
kombu==4.5.0
pytz==2019.1
redis==3.2.1
tzlocal==1.5.1
vine==1.3.0
zope.interface==4.6.0
源文件:
# celery_task.py
from celery import Celery
from celery.schedules import crontab
from tzlocal import get_localzone
app = Celery('crontab_task', broker='redis://localhost:6379/0')
app.conf.timezone = get_localzone() # 获得本地时区,比pytz要好用
@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)
发布任务:
celery -A celery_task beat
下面为成功显示信息
celery beat v4.3.0 (rhubarb) is starting.
__ - ... __ - _
LocalTime -> 2019-04-11 15:27:17
Configuration ->
. broker -> redis://localhost:6379/0
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%WARNING
. maxinterval -> 5.00 minutes (300s)
执行任务:
celery -A celery_task worker --loglevel=info