简体   繁体   English

分离芹菜消费者和生产者

[英]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. 我正在使用带有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). 所以我想知道是否有一种方法可以配置芹菜,以便在一个项目中我有Flask应用程序将消息发送到队列(生产者)。 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? 我是否将API(发送电子邮件)放入我的烧瓶应用程序或芹菜项目中? Ultimately I would like to have the Flask application and the Celery instance in different EC2 instances - with rabbitmq acting as the message broker. 最后,我想将Flask应用程序和Celery实例放在不同的EC2实例中 - 使用rabbitmq充当消息代理。

Thanks for your help! 谢谢你的帮助!

You can use Celery's send_task function to send the task through RabbitMQ to the worker using the task name. 您可以使用Celery的send_task函数通过RabbitMQ使用任务名称将任务发送给worker。 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. 如果任务未在当前进程中注册,则可以使用send_task()来代替调用任务。

Example: 例:

from yourmodule.yourapp import celery
celery.send_task("yourtasksmodule.yourtask", args=["Hello World"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM