Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pragmatically schedule Celery task with dynamic inputs?

I have this situation where I need to pragmatically schedule a task, and the task definition requires certain input parameters each time it needs to be executed. Consider the following task function:

@task
def add_numbers(a, b):
   sum = a + b
   return sum

Now if I need to run the above task at two different time instances with different input parameters ex T1->add_numbers(4,5) and T2->add_numbers(2,2), how do I go about it. Thanks for the help.

ENV: Django 1.4, Celery 3.0

like image 965
Zakiullah Khan Avatar asked Mar 06 '26 00:03

Zakiullah Khan


1 Answers

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.

like image 60
fandyst Avatar answered Mar 07 '26 14:03

fandyst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!