代码
s4.py
from celery import Celery
from celery.schedules import crontab
app = Celery('tasks', broker='redis://127.0.0.1:6379', backend='redis://127.0.0.1:6379')
@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)
@app.task
def add(x, y):
z = x + y
print(z)
命令
cd s4.py 所在的目录
# 第一个终端,启动 worker
celery -A s4 worker -l info
# 第二个终端,启动 心跳
celery -A s4 beat -l info
# 说明:
# -A main 找到项目
# -l info 是打印日志log,代码上线时不加info