[Django]-How to pragmatically schedule Celery task with dynamic inputs?

2๐Ÿ‘

I encountered this problem too, and I think there is another way to help you out.
the answer of @narced133 works only if the varies of params are limited, then what if we have thousands kinds of params?

solution:
refactor your celery task to accept a function as param, and the function will generate or get the params that are the real params for your celery task: add_numbers.

๐Ÿ‘คfandyst

1๐Ÿ‘

See the Celery documentation on periodic tasks for an example of how to call a task with arguments at a specified interval. http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries

In your example, you could use something like:

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'T1': {
        'task': 'tasks.add_numbers',
        'schedule': crontab(hour=7, minute=30, day_of_week=1),,
        'args': (4, 5)
    },
    'T2': {
        'task': 'tasks.add_numbers',
        'schedule': crontab(hour=16, minute=0),,
        'args': (2, 2)
    },
}
๐Ÿ‘คnarced133

Leave a comment