Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery : Execute task after a specific time gap

I want to send an email to my users exactly 48 hours after they have registered.How do i achieve this using celery? If I create a periodic task to send an email, I will have to decide a specific time during which i want to execute that task. I don't want to keep run a celery task every second to check if there are any emails needed to be sent.

like image 304
mukesh Avatar asked Jun 04 '14 10:06

mukesh


People also ask

How do you set time on Celery?

To schedule task you need to use celery beat . You can schedule your task at any specific time using periodic task . To know more use this link https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html . Don't forget to restart your celery beat after creation of task .

How do you schedule a Celery task in Python?

To begin, let's first set up the Flask framework with Celery. Save this python script as app.py in the main project directory, and in your terminal, run: python ~/celery-scheduler/app.py . Now go to http://localhost:5000/. If everything is running fine, you should see “Hello, Flask is up and running!”

Are celery tasks asynchronous?

Once you integrate Celery into your app, you can send time-intensive tasks to Celery's task queue. That way, your web app can continue to respond quickly to users while Celery completes expensive operations asynchronously in the background.

How does Celery retry work?

In the above example, the task will retry after a 5 second delay (via countdown ) and it allows for a maximum of 7 retry attempts (via max_retries ). Celery will stop retrying after 7 failed attempts and raise an exception.


1 Answers

You'll want to make use of ETA. Read that section of the docs as it'll have more information. However, your code will look something like this:

from datetime import datetime, timedelta
send_date = datetime.utcnow() + timedelta(days=2)
email_user.apply_async([user], eta=send_date)
like image 168
schillingt Avatar answered Oct 07 '22 15:10

schillingt