Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 可以向循环中的和弦添加任务吗?_Python_Celery - Fatal编程技术网

Python 可以向循环中的和弦添加任务吗?

Python 可以向循环中的和弦添加任务吗?,python,celery,Python,Celery,我发现的大多数和弦示例都是这样的: res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s()) 或 我目前有一段代码正在启动任务: for node in self.simulationRecord.fields['departureNodes']: passengers = int(outgoing_seat_counts.get(node, 0)) # send the job to the queue res =

我发现的大多数和弦示例都是这样的:

res = chord([add.s(2, 2), add.s(4, 4)])(sum_task.s())

我目前有一段代码正在启动任务:

for node in self.simulationRecord.fields['departureNodes']:
    passengers = int(outgoing_seat_counts.get(node, 0)) 
    # send the job to the queue
    res = tasks.my_task.delay(passengers, start_date, end_date)
    task_ids.append(res.id)
我想转换为使用和弦,但根据我看到的示例,我不太清楚如何将当前结构转换为使用和弦。如何将每个调用添加到我的任务中,并将参数添加到chord?

您可以从任务列表和回拨中调用chord。例如,如果您有一个add函数,那么您可以像这样调用chord

tasks = []

for i in range(10):
    tasks.append(add.s(i, i))

callback = add.si(100, 100)

chord_result = group(tasks)(callback)

最后我把代码改成了下面的和弦。基本上,我在循环中构建参数列表,然后使用该列表创建和弦:

arg_list = []
for node in self.simulationRecord.fields['departureNodes']:
    passengers = int(outgoing_seat_counts.get(node, 0)) 
    arg_list.append({'passengers': passengers})
res = chord(tasks.my_task.s(i['passengers'],start_date,end_date) for i in arg_list)(tasks.callback.s())
task_ids = [task.id for task in res.parent.results]
arg_list = []
for node in self.simulationRecord.fields['departureNodes']:
    passengers = int(outgoing_seat_counts.get(node, 0)) 
    arg_list.append({'passengers': passengers})
res = chord(tasks.my_task.s(i['passengers'],start_date,end_date) for i in arg_list)(tasks.callback.s())
task_ids = [task.id for task in res.parent.results]