创建芹菜任务然后同步运行 [英] Create celery tasks then run synchronously
问题描述
我的应用程序在页面上收集一堆电话号码。一旦用户点击提交按钮,我创建一个芹菜任务来调用每个号码并发出提醒消息,然后将其重定向到一个页面,在那里他们可以看到有关呼叫的实时更新。我正在使用网络套接字来更新每个呼叫的状态,并需要同步执行任务,因为我只能从一个号码拨出。
My app gathers a bunch of phone numbers on a page. Once the user hits the submit button I create a celery task to call each number and give a reminder message then redirect them to a page where they can see the live updates about the call. I am using web sockets to live update the status of each call and need the tasks to execute synchronously as I only have access to dial out from one number.
所以一旦第一个通话/任务完成,我想要下一个通话。
So once the first call/task is completed, I want the next one to fire off.
我看了一下 CELERY_ALWAYS_EAGER 设置,但它刚刚通过第一次迭代并停止。
I took a look at CELERY_ALWAYS_EAGER settings but it just went through the first iteration and stopped.
@task
def reminder(number):
# CODE THAT CALLS NUMBER HERE....
def make_calls(request):
for number in phone_numbers:
reminder.delay(number)
return redirect('live_call_updates')
推荐答案
如果你要一个接一个地打电话,为什么不你在一个任务中包装所有的电话
if you want to fire each call one after another, why dont you wrap all the calls in one task
@task
def make_a_lot_of_calls(numbers):
for num in numbers:
# Assuming that reminder blocks till the call finishes
reminder(number)
def make_calls(request):
make_a_lot_of_calls.delay(phone_numers)
return redirect('live_call_updates')
这篇关于创建芹菜任务然后同步运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!