4
我有一个API,它返回其他API的列表。执行其他芹菜任务不工作的芹菜周期性任务
我需要访问这些API每隔15分钟,并把返回到数据库中的数据。
下面是我用芹菜和Redis的在celery_worker.py文件中写道。但是所有的任务都没有开始。
list_of_APIs = requests.get(the_api_that_returns_list_of_APIs).json()
CELERYBEAT_SCHEDULE = {
'every-15-minute': {
'task': 'fetch_data_of_all_APIs',
'schedule': timedelta(minutes=15),
},
}
@celery.task
def access_one_API(one_API):
return requests.get(one_API).json()
@celery.task(name='fetch_data_of_all_APIs')
def fetch_data_of_all_APIs():
for one_API in list_of_APIs:
task = access_one_API.delay(one_API)
# some codes to put all task.id into a list_of_task_id
for task_id in list_of_task_id:
# some codes to get the results of all tasks
# some codes to put all the results into a database
的fetch_data_of_all_APIs
功能应每15分钟,这是应该使用多个工人跑access_one_API
功能
芹菜服务器启动成功的终端但既不fetch_data_of_all_APIs
也不access_one_API
开始运行。
如果我提取fetch_data_of_all_APIs
函数中的代码,access_one_API
可以启动并由多个芹菜工作人员执行。但只要我将这些代码放在一个函数中并用@celery.task
来修饰它,那么这两个函数都不会启动。
所以我相信它一定与芹菜有关。
非常感谢提前。
请注意,您需要'@ celery.task()'装饰器。另外,您需要检查'celery-beat'配置参数,因为当前的芹菜版本使用小写设置。 –