Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to start a task at a specific time with django & celery

I'm using Celery, and it works for async, but I need to set up an operation to a specific datetime.

For example "on the 30th of August, 2019, at 11:35, do this."

My celery.py is very simple now, but it works:

import time
from datetime import datetime, timedelta
from datetime import date
from celery import shared_task,current_task, task
from celery import Celery

app = Celery()

@app.task
def test():

    print ('1')
    todaynow = datetime.now()

    print todaynow

I call it from view and run print on rabbit

Any idea for how to program a task?

ty

EDIT:

I try in view to call "test"

test.apply_async(eta=datetime(2019, 7, 31, 6, 28))

in flower it receive the task but not execute it, why?

enter image description here

like image 673
JuConte Avatar asked Jul 30 '19 09:07

JuConte


People also ask

How do I schedule a task in Django?

Run the scheduler When using Django, you will usually have one process that is responsible for serving web requests and a separate one that takes care of processing tasks. During local development, these two processes are: web requests: ./manage.py runserver. async tasks: ./manage.py qcluster.

What is periodic task in Django?

As you build and scale a Django app you'll inevitably need to run certain tasks periodically and automatically in the background. Some examples: Generating periodic reports. Clearing cache. Sending batch e-mail notifications.

How Celery beat works?

celery beat is a scheduler. It kicks off tasks at regular intervals, which are then executed by the worker nodes available in the cluster. By default the entries are taken from the CELERYBEAT_SCHEDULE setting, but custom stores can also be used, like storing the entries in an SQL database.

What is asynchronous task in Django?

An async view function in Django is detected by the annotation async def , which then runs the async view in a thread within its own event loop. This gives the benefit of being able to do and run tasks concurrently inside the async views.


1 Answers

To run a task at a specific time you can pass the eta parameter to apply_async

test.apply_async(eta=datetime.datetime(2019, 8, 30, 11, 35))
like image 60
Iain Shelvington Avatar answered Sep 22 '22 00:09

Iain Shelvington