我需要将其他参数传递给芹菜和弦中的回调函数。 ( Celery版本:4.1.0(latentcall)和Python 2.7 )
考虑以下示例:
program.py
from tasks import get_stock_info, call_back
from celery import group, chord
def chord_queue():
header = (get_stock_info.subtask((delay,)) for delay in [4, 5, 4])
callback = call_back.subtask()
header1 = (get_stock_info.subtask((delay,)) for delay in [4, 4, 4])
res = chord(header,queue='susanoo_dev')(callback)
res1 = chord(header1,queue='susanoo_core')(callback)
print(res.get())
print(res1.get())
print("We are done")
if __name__ == '__main__':
chord_queue()
tasks.py
from pandas_datareader import data
from celery_app import app
import time
@app.task
def get_stock_info(delay):
print('hello Celery--------')
time.sleep(delay)
print('Whats up')
return 10
@app.task
def call_back(num):
print("Everything is done------")
print("Everything is done------")
return sum(num)
celery_app.py
from celery import Celery
from kombu import Queue
app = Celery('tasks', broker='amqp://my_user:my_pass@localhost/my_vhost', backend='redis://localhost:6379/0')
CELERY_CONFIG = {
'CELERY_DEFAULT_QUEUE': 'default',
'CELERY_QUEUES': (Queue('dev'), Queue('core'),)
}
app.conf.update(**CELERY_CONFIG)
现在在这种情况下,当和弦被调用并且在所有3 get_stock_info 任务完成之后, call_back 被调用,值 10 ,这是 get_stock_info 的返回值自动传递。 现在连同返回值我还想传递一个额外的参数,将一个字符串称为" abcd"到回调函数。
我该怎么做?
我已按照某些博客/ SO答案等建议尝试这样做。
program.py
def chord_queue():
header = (get_stock_info.subtask((delay,)) for delay in [4, 5, 4])
callback = call_back.subtask(kwargs={'my_str' : 'abcd'})
header1 = (get_stock_info.subtask((delay,)) for delay in [4, 4, 4])
res = chord(header,queue='susanoo_dev' )(callback)
res1 = chord(header1,queue='susanoo_core')(callback)
print(res.get())
print(res1.get())
print("We are done")
tasks.py
@app.task
def call_back(num, my_str):
print("Everything is done------")
print("Everything is done------")
print my_str
return my_str, sum(num)
但这似乎不起作用并引发以下错误:
celery.backends.base.ChordError:回调错误:TypeError(" call_back()得到了一个意外的关键字参数' my_str'",)
答案 0 :(得分:1)
得到了答案。感谢一位帮助我的朋友。
在上述解决方案中所做的一切都是在my_str
的定义中没有将call_back()
定义为关键字参数。
所以工作解决方案是:
<强> program.py 强>
def chord_queue():
header = (get_stock_info.subtask((delay,)) for delay in [4, 5, 4])
callback = call_back.subtask(kwargs={'my_str' : 'abcd'})
header1 = (get_stock_info.subtask((delay,)) for delay in [4, 4, 4])
res = chord(header,queue='susanoo_dev' )(callback)
res1 = chord(header1,queue='susanoo_core')(callback)
print(res.get())
print(res1.get())
print("We are done")
<强> task.py 强>
@app.task
def call_back(num, my_str=None):
print("Everything is done------")
print("Everything is done------")
print my_str
return my_str, sum(num)
它没有任何问题,按预期工作。