Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating celery consumer and producer

I want my email service that I wrote to be completely decoupled from my flask application. I am using celery with rabbitmq. So I am wondering is there a way I can configure celery so that in one project I have the Flask application that sends the message to the queue (producer). And in another project I have the celery instance running that listens to the message and execute the task(consumer). I am still confused by how the communication will exactly work? Do I put the API (that sends the email) in my flask application OR the celery project? Ultimately I would like to have the Flask application and the Celery instance in different EC2 instances - with rabbitmq acting as the message broker.

Thanks for your help!

like image 325
user2216194 Avatar asked Oct 28 '13 19:10

user2216194


People also ask

Is celery a producer or consumer?

Celery act as both the producer and consumer of RabbitMQ messages. In Celery, the producer is called client or publisher and consumers are called as workers. It is possible to use a different custom consumer (worker) or producer (client). RabbitMQ or AMQP message queues are basically task queues.

How does celery distribute?

Celery is a distributed task queue written in Python, which works using distributed messages. Each execution unit in celery is called a task. A task can be executed concurrently on one or more servers using processes called workers.

Why does celery need a broker?

The broker is the third-person facilitator between a buyer and a seller. Celery requires a solution to send and receive messages; usually, this comes in the form of a separate service called a message broker. In celery, the broker is Redis, RabbitMQ, etc who conveying the message between a client and celery.


1 Answers

You can use Celery's send_task function to send the task through RabbitMQ to the worker using the task name. You still need to import the module that you have the celery app in:

If the task is not registered in the current process you can use send_task() to call the task by name instead.

Example:

from yourmodule.yourapp import celery
celery.send_task("yourtasksmodule.yourtask", args=["Hello World"])
like image 102
Lycha Avatar answered Sep 19 '22 15:09

Lycha