Add dynamic task to celery

Viewed 332

I am trying to find a way to add a new task to celery after celery worker has been executed and after celery has been instantiated. I basically want to add a new task with a dynamic name based on user input. I will also want to set a rate limit for that new task.

I have not been able to find any documentation on this and no examples on my google searches. All I have been able to find is dynamically adding periodic tasks with celery beat.

Is there any way to do what I am looking to do?

2 Answers

What you want to achieve is not trivial. I am not aware of any distributed system similar to Celery that allows such thing.

The only way perhaps to do it is to dynamically create and run a new Celery worker with the new task added and configured the way you prefer...

You can register a task as:

def dynamic_task():
   return "Hi"

dynamic_task = app.task(dynamic_task, name='my_name')

See that you can register a list of functions in this way, then you will need to restart the worker, however you can use the signals https://docs.celeryproject.org/en/stable/userguide/signals.html as well.

Related