目标:使用celery实现一个周期性任务,每隔10s打印一次"hello,world"
1.创建一个目录my_scheduler
2.在该目录下创建两个文件
test/my_scheduler/celery.py
from celery import Celery
app = Celery('my_scheduler', broker='redis://127.0.0.1:6379/0', backend='redis://127.0.0.1:6379/0')
app.autodiscover_tasks(['my_scheduler.celery_task'])
app.conf.beat_schedule = {
'hello-every-10-seconds': {
'task': 'my_scheduler.celery_task.hello',
'schedule': 10.0,
},
}
test/my_scheduler/celery_task.py
from celery import Celery
from celery.schedules import crontab
# app = Celery('my_scheduler', broker='redis://localhost:6379/0')
from celery import shared_task
@shared_task
def hello():
print('Hello, world!')
3.在test下执行
celery -A my_scheduler worker -l info -P eventlet
celery -A my_scheduler beat
遇到问题1:
app.autodiscover_task()执行报错
Received unregistered task of type 'my_scheduler.celery_task.hello'.
The message has been ignored and discarded.
Did you remember to import the module containing this task?
Or maybe you're using relative imports?
Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.
可以看到是因为发送的任务未注册导致,需要指定注册的目录:app.autodiscover_tasks([‘my_scheduler.celery_task’])
遇到问题2:
celery -A my_scheduler worker -l info启动任务报错
[2023-09-18 15:22:02,071: INFO/MainProcess] Task my_scheduler.celery_task.hello[88cf347f-5766-4cac-9816-e832f90d2c4a] received
[2023-09-18 15:22:02,113: ERROR/MainProcess] Task handler raised error: ValueError('not enough values to unpack (expected 3, got 0)')
Traceback (most recent call last):
File "D:\APP_install\python\lib\site-packages\billiard\pool.py", line 362, in workloop
result = (True, prepare_result(fun(*args, **kwargs)))
File "D:\APP_install\python\lib\site-packages\celery\app\trace.py", line 638, in fast_trace_task
tasks, accept, hostname = _loc
ValueError: not enough values to unpack (expected 3, got 0)
查询原因:
Eventlet has an issue on subprocess.CalledProcessError:
https://github.com/celery/celery/issues/4063
https://github.com/eventlet/eventlet/issues/357
https://github.com/eventlet/eventlet/issues/413
解决方法:
pip install gevent
celery -A <module> worker -l info -P gevent