[英]Python Celery subtask group not executing
I'm trying to use Celery to handle background tasks. 我正在尝试使用Celery处理后台任务。 I currently have the following setup: 我目前有以下设置:
@app.task
def test_subtask(id):
print('test_st:', id)
@app.task
def test_maintask():
print('test_maintask')
g = group(test_subtask.s(id) for id in range(10))
g.delay()
test_maintask
is scheduled to execute every n seconds, which works (I see the print statement appearing in the command line window where I started the worker). test_maintask
计划每隔n秒执行一次,这可以正常工作(我在启动工作程序的命令行窗口中看到打印语句)。 What I'm trying to do is have this scheduled task spawn a series of subtasks, which I've grouped here using group()
. 我想做的是让此计划任务产生一系列子任务,这些子任务已在此处使用group()
分组。
It seems, however, like none of the test_subtask
tasks are being executed. 但是,似乎没有任何test_subtask
任务正在执行。 What am I doing wrong? 我究竟做错了什么? I don't have any timing/result constraints for these subtasks and just want them to happen some time from now, asynchronously, in no particular order. 我对这些子任务没有任何时间/结果约束,只是希望它们从现在开始以某种特定的顺序异步发生。 n seconds later, test_maintask
will fire again (and again) but with none of the subtasks executing. n秒后, test_maintask
将再次触发(并再次触发),但没有test_maintask
任务执行。
I'm using one worker, one beat, and AMQP as a broker (on a separate machine). 我正在使用一个工作人员,一个节拍和AMQP作为经纪人(在另一台机器上)。
EDIT : For what it's worth, the problem seems to be purely because of one task calling another (and not something because of the main task being scheduled). 编辑 :对于它的价值,问题似乎纯粹是由于一个任务调用了另一个任务(而不是由于计划了主要任务而导致的问题)。 If I call the main task manually: 如果我手动调用主要任务:
celery_funcs.test_maintask.delay()
I see the main task's print statement but -- again -- not the subtasks. 我看到了主任务的打印语句,但又看到了子任务。 Calling a subtask directly does work however: 但是直接调用子任务确实可以工作:
celery_funcs.test_subtask.delay(10)
Sigh... just found out the answer, I used the following to configure my Celery app: igh ...刚找到答案,我使用以下命令配置了Celery应用程序:
app = Celery('celery_app', broker='<my_broker_here>')
Strangely enough, this is not being picked up in the task itself... that is, 奇怪的是,任务本身并没有意识到这一点……
print('test_maintask using broker', app.conf.BROKER_URL, current_app.conf.BROKER_URL)
Gives back '<my_broker_here>'
and None
respectively, causing the group to be send of to... some default broker (I guess?). 分别返回'<my_broker_here>'
和“ None
,从而导致该组被发送给...一些默认代理(我想呢?)。
Adding BROKER_URL
to app.conf.update
does the trick, though I'm still not completely clear on what's going on in Celery's internals here... 将BROKER_URL
添加到app.conf.update
可以解决问题,尽管我仍然不太清楚Celery内部的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.