Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create celery tasks then run synchronously

Tags:

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.

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')  
like image 588
Austin Avatar asked Oct 07 '14 17:10

Austin


People also ask

Are Celery tasks asynchronous?

Celery is a task queue/job queue based on asynchronous message passing. It can be used as a background task processor for your application in which you dump your tasks to execute in the background or at any given moment. It can be configured to execute your tasks synchronously or asynchronously.

Can a celery task call another task?

To answer your opening questions: As of version 2.0, Celery provides an easy way to start tasks from other tasks. What you are calling "secondary tasks" are what it calls "subtasks".

How do you run Celery beat in the background?

In production you should use one of them to run celery workers + celery beat in background. Simplest way to check celery beat is running: ps aux | grep -i '[c]elerybeat' . If you get text string with pid it's running.


1 Answers

If you look at the celery DOCS on tasks you see that to call a task synchronosuly, you use the apply() method as opposed to the apply_async() method.

So in your case you could use:

 reminder.apply(args=[number]) 

The DOCS also note that:
If the CELERY_ALWAYS_EAGER setting is set, it will be replaced by a local apply() call instead.

Thanks to @JivanAmara who in the comments reiterated that when using apply(), the task will run locally(in the server/computer in which its called). And this can have ramifications, if you intended to run your tasks across multiple servers/machines.

like image 130
Komu Avatar answered Oct 07 '22 07:10

Komu