简体   繁体   中英

How to schedule a celery task through a django view

Is there any way to define a date and time in a django view and run a celery task at the defined time?

For example

def test(request):
    date = '2020-09-12'
    time = '11:34'
    
    # run the below task at the specified time
    test_celery_test.delay()
    
    ...

I want the task to run only once at the specified time and should not repeat

Can you try this below

Lets say this is your task

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

Then you can call like this in your views.py

test.apply_async(eta=datetime.datetime(2019, 8, 30, 11, 35))

If you just want to run it once, why do you want to put in your views.py, i would suggest you to look for this same thing in custom management commond or crontab for this.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM