django+rabbitmq
时间: 2023-08-18 18:07:43 浏览: 117
在你的Django项目中使用RabbitMQ替代Redis和Celery的步骤如下:
1. 首先,创建一个名为`init_mq.py`的文件,并将以下内容添加到文件中:
```
from django.core.management.base import BaseCommand
from MyRab.management.untils.Rabbitmqserver import RabbitmqClient
class Command(BaseCommand):
def handle(self, *args, **options):
print("init_mq", "开始初始化Rabbitmq队列")
try:
RabbitmqClient.connent()
RabbitmqClient.channel.queue_declare(queue='send_result', durable=True)
RabbitmqClient.channel.queue_declare(queue='backend', durable=True)
RabbitmqClient.channel.queue_declare(queue='intelligent', durable=True)
RabbitmqClient.channel.queue_declare(queue='resume', durable=True)
print("init_mq", "初始化Rabbitmq队列成功")
except Exception as e:
print("init_mq",e,"队列初始化失败")
```
这个脚本会初始化RabbitMQ队列。
2. 接下来,创建一个名为`intelligentAduit_listener.py`的文件,并将以下内容添加到文件中:
```
from django.core.management.base import BaseCommand
from YouRab.management.untils.Rabbitmqserver import RabbitmqClient
from YouRab.views import save_student_info, save_class_info
class Command(BaseCommand):
def handle(self, *args, **options):
def parse_result_func(ch, method, properties, body):
res = save_class_info(body)
ch.basic_ack(delivery_tag=method.delivery_tag)
print("parse_result_func", "开始监听班级信息")
RabbitmqClient.connent()
RabbitmqClient.expense("intelligent", parse_result_func)
```
这个脚本会监听算法端返回的消息,并将结果存入数据库。
3. 在你的Django项目中,运行以下命令初始化RabbitMQ队列:
```
python manage.py init_mq
```
4. 运行以下命令启动监听器:
```
python manage.py intelligentAduit_listener
```
通过以上步骤,你就可以在Django中使用RabbitMQ来执行耗时任务了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [django中使用RabbitMQ](https://blog.csdn.net/qq_37623764/article/details/105825554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [django项目中Rabbitmq的使用,两个项目之间数据传输存储](https://blog.csdn.net/weixin_44706011/article/details/107143561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文