简体   繁体   中英

Manually running celery scheduled tasks within Django

In my Django settings file I have something that looks like this:

CELERY_BEAT_SCHEDULE = {
    'my_task': {
        'task': 'tasks.my_task',
        'schedule': crontab(hour=4, minute=0),
        'kwargs': {'interval': 'hour', 'features': [], 'max_samples': 200,
                   'training_days': 90, 'force_update': False},
        'options': {'queue': 'my_queue'},
    },
}

What I would like to do is to be able to manually have this task asynchronously start. One way I know of to do this is:

task = settings.CELERY_BEAT_SCHEDULE['my_task']
tasks.my_task.apply_async(kwargs=task['kwargs'], queue=task['options']['queue'])

while this works it is a bit clunky for my tastes and I am looking for a better way.

Is there a way to find celery's interpretation of the scheduled tasks defined in CELERY_BEAT_SCHEDULE and manually running them from there?


The closest I have found so far is inspect().registered_tasks() from celery.task.control but that seems to be more about the workers than the scheduled tasks.

I'm 95% sure I'm missing an important aspect of this but I'll give it a shot.

If you want to run a specific task async but kick it off yourself, can you not just do your_task.delay() ?

If you want to run all your scheduled tasks manually, just parse CELERY_BEAT_SCHEDULE from django.conf.settings , importing each task and running .delay() on it.

EDIT: you seem to be doing something similar in your example. Are you able to say why you don't like this solution?

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