让芹菜等待任务完成
Make celery wait for task to finish
我想让 celery 等待特定任务完成,因此我在 celery 本身旁边安装了 celery-results-backend。但是我不明白我必须如何编写我的任务调用才能等待,因为我目前收到以下错误:
example_task() missing 1 required positional argument: 'user_pk'
views.py:
def example(request):
user = request.user
if request.method == 'GET':
result = example_taks.apply_async(user_pk=user.pk)
result_output = result.wait(timeout=None, interval=0.5)
return redirect('something')
else:
args = {'user': user}
return redirect(reverse('something'), args)
tasks.py:
def example_task(user_pk):
user = User.objects.get(pk=user_pk)
try:
...
之前我是这样称呼会谈的:
def example(request):
user = request.user
if request.method == 'GET':
example_task.delay(request.user.pk)
...
这工作正常,但没有等待任务完成。
如果我这样做:
result = allocate_new_bch_address.apply_async(request.user.pk)
我也得到一个错误:
example_task() argument after * must be an iterable, not UUID
首先,您使用 apply_async()
错误。如 here. This is because you can specify additional parameters that define how the task should run. On the other hand, delay()
only accepts your task's args and kwargs 所示,该函数接受打包为元组 (args) 和字典 (kwargs) 的任务参数。 delay()
在大多数情况下就足够了。
您可以这样做:
example_taks.apply_async(kwargs={"user_pk":user.pk})
或者这样:
example_tasks.delay(user_pk=user.pk)
您也可以使用位置参数,但我建议尽可能使用 kwargs。
其次,一旦提交就等待异步任务违背了 Celery 的目的。要等待任务完成,您需要对结果调用 get():
result = example_tasks.apply_async(kwargs={"user_pk":user.pk})
result_output = result.get()
这就是你等待结果的方式。
result = example_tasks.apply_async(kwargs={"user_pk": user.pk})
# Do some other things while celery is running with the task
# Sync up with the task
result.wait(timeout=10) # seconds
if result.result == 1:
print("Do something")
我想让 celery 等待特定任务完成,因此我在 celery 本身旁边安装了 celery-results-backend。但是我不明白我必须如何编写我的任务调用才能等待,因为我目前收到以下错误:
example_task() missing 1 required positional argument: 'user_pk'
views.py:
def example(request):
user = request.user
if request.method == 'GET':
result = example_taks.apply_async(user_pk=user.pk)
result_output = result.wait(timeout=None, interval=0.5)
return redirect('something')
else:
args = {'user': user}
return redirect(reverse('something'), args)
tasks.py:
def example_task(user_pk):
user = User.objects.get(pk=user_pk)
try:
...
之前我是这样称呼会谈的:
def example(request):
user = request.user
if request.method == 'GET':
example_task.delay(request.user.pk)
...
这工作正常,但没有等待任务完成。
如果我这样做:
result = allocate_new_bch_address.apply_async(request.user.pk)
我也得到一个错误:
example_task() argument after * must be an iterable, not UUID
首先,您使用 apply_async()
错误。如 here. This is because you can specify additional parameters that define how the task should run. On the other hand, delay()
only accepts your task's args and kwargs 所示,该函数接受打包为元组 (args) 和字典 (kwargs) 的任务参数。 delay()
在大多数情况下就足够了。
您可以这样做:
example_taks.apply_async(kwargs={"user_pk":user.pk})
或者这样:
example_tasks.delay(user_pk=user.pk)
您也可以使用位置参数,但我建议尽可能使用 kwargs。
其次,一旦提交就等待异步任务违背了 Celery 的目的。要等待任务完成,您需要对结果调用 get():
result = example_tasks.apply_async(kwargs={"user_pk":user.pk})
result_output = result.get()
这就是你等待结果的方式。
result = example_tasks.apply_async(kwargs={"user_pk": user.pk})
# Do some other things while celery is running with the task
# Sync up with the task
result.wait(timeout=10) # seconds
if result.result == 1:
print("Do something")