Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke Celery task by name

Tags:

python

celery

We have a Python application that uses Celery, with RabbitMQ as the Broker. Think of this application as a management application and only puts messages/tasks into the Broker and won't be acting upon them.

There will be another application (which may or may not be Python based) which will be acting upon the messages.

Is it possible for the management application to put a message/task on a Queue when that task doesn't exist in it's codebase? If so, how would I go about this?

like image 875
justcompile Avatar asked Nov 30 '22 17:11

justcompile


2 Answers

There is a more "Celery-esque" way called Signatures. Setup a Celery app pointing to the same broker and create a signature for your task:

from celery import Celery

celeryapp = Celery(...)

my_task = celeryapp.signature('task.add')
result = my_task.delay(2, 2)
print result.get()
like image 139
tuomur Avatar answered Dec 03 '22 06:12

tuomur


  1. Create a new instance of Celery or Import from a module.
  2. Call send_task() method.

    from twittershell.core import app # Celery Instance from a module
    async_result = app.send_task(name='tasks.followers', args=(529675892, ))
    

The signature of send_task method is:

    def send_task(self, name, args=None, kwargs=None, countdown=None,
              eta=None, task_id=None, producer=None, connection=None,
              router=None, result_cls=None, expires=None,
              publisher=None, link=None, link_error=None,
              add_to_parent=True, reply_to=None, **options):
like image 39
guneysus Avatar answered Dec 03 '22 06:12

guneysus