Answer the question
In order to leave comments, you need to log in
How to collect the execution of all tasks from one function for further response to the user?
Greetings,
the task is to wait for the execution of all tasks with a further response to the client, a code example for a more specific understanding
@task(base=NotifierTask)
first(*args):
# runs some time
return json_resp
@task(base=NotifierTask)
second(*args):
# runs some time
return json_resp
@task(base=NotifierTask)
third(*args):
# runs some time
return json_resp
# На этот урл идет запрос
# количество выполнения ф-ций может варьироваться в зависимости от запроса
# может выполниться от 1 до 40 ф-ций
def main(request):
# there we get some data
data = request.POST.get('data')
for some_func in data:
if some_func == 'first':
first.delay(args)
elif some_func == 'second':
second.delay(args)
elif some_func == 'third':
third.delay(args)
# Все это дело попадает на воркер селери
# Notifier Task
class NotifierTask(Task):
"""
Tasks that sends notification on completion.
"""
abstract = True
def after_return(self, status, retval, task_id, args, kwargs, einfo):
# Получаем результат выполнения и прокидываем через сокет на сторону клиента
@task(base=NotifierTask)
def celery_test(id, position):
time.sleep(id)
return "Time delay: %s, Position: %s" % (str(id), str(position))
def some_request(request):
from django.http import JsonResponse
if request.method == 'GET':
# tasks_list = []
# count = 0
# while count < 10:
# count += 1
# tasks_list.append(celery_test.s(count))
#
# job = group(tasks_list)
job = group([
celery_test.s(10, 1),
celery_test.s(2, 2),
celery_test.s(2, 3),
celery_test.s(2, 4),
celery_test.s(5, 5),
])
job.apply_async()
return JsonResponse(dict(success='Done'))
Answer the question
In order to leave comments, you need to log in
we collect tasks in a sheet and feed it all into the chord function
task_ = []
task_.append(clear_old_project.s(api_key=api_key))
for group_ in list_all_groups:
task_.append(frequency_start.s(api_key=api_key,
job_request_id=job_request_id,
app_secret=app_secret,
project_id=proj_id,
group_id=group_.id,
providers=providers,
region_key=region_key,
phrase_forms=phrase_forms))
chord(task_)(generate_reports_frequency.s(api_key, proj_id, job_request_id, app_secret, providers, region_key,o_currency))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question