当前位置: 首页 > news >正文

wordpress 微媒体插件佛山seo培训机构

wordpress 微媒体插件,佛山seo培训机构,静态网站 挂马,网站关键字收录介绍 RabbitMQ 是一个消息中间件,它实现了 AMQP (Advanced Message Queuing Protocol) 协议。本教程将引导你通过几个简单的步骤来学习如何使用 RabbitMQ 发送和接收消息。 环境准备 1. 安装 RabbitMQ - 在你的系统上安装 RabbitMQ: https://www.rabbitmq.com/d…

介绍

RabbitMQ 是一个消息中间件,它实现了 AMQP (Advanced Message Queuing Protocol) 协议。本教程将引导你通过几个简单的步骤来学习如何使用 RabbitMQ 发送和接收消息。

环境准备

1. 安装 RabbitMQ

- 在你的系统上安装 RabbitMQ: https://www.rabbitmq.com/download.html

- 启动服务: `sudo rabbitmq-server`

2. 安装客户端库

- Python 示例将使用 `pika` 库: `pip install pika`

第一步: 创建生产者

创建一个简单的生产者,用于发送消息到 RabbitMQ 服务器。

```python

import pika

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

message = "Hello World!"

channel.basic_publish(exchange='',

routing_key='hello',

body=message)

print(" [x] Sent %r" % message)

connection.close()

if __name__ == '__main__':

main()

```

第二步: 创建消费者

创建一个简单的消费者,用于接收来自 RabbitMQ 服务器的消息。

```python

import pika

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

if __name__ == '__main__':

main()

```

第三步: 使用持久化消息

确保消息在 RabbitMQ 重启后仍然存在。

生产者代码修改

```python

import pika

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello', durable=True)

message = "Hello World!"

channel.basic_publish(exchange='',

routing_key='hello',

body=message,

properties=pika.BasicProperties(

delivery_mode=2, # make message persistent

))

print(" [x] Sent %r" % message)

connection.close()

if __name__ == '__main__':

main()

```

消费者代码修改

```python

import pika

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello', durable=True)

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=False)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

if __name__ == '__main__':

main()

```

第四步: 使用工作队列

实现一个简单的工作队列,可以分发任务给多个工作者。

生产者

```python

import pika

import sys

import random

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = ' '.join(sys.argv[1:]) or "Hello World!"

message += f" {random.randint(1, 10)}"

channel.basic_publish(

exchange='',

routing_key='task_queue',

body=message,

properties=pika.BasicProperties(delivery_mode=2)) # make message persistent

print(" [x] Sent %r" % message)

connection.close()

if __name__ == '__main__':

main()

```

工作者

```python

import pika

import time

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

time.sleep(body.count(b'.'))

print(" [x] Done")

ch.basic_ack(delivery_tag=method.delivery_tag)

def main():

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue',

on_message_callback=callback)

channel.start_consuming()

if __name__ == '__main__':

main()

```

http://www.ds6.com.cn/news/57905.html

相关文章:

  • 迪拜哪个网站是做色情的企业全网推广
  • 东道网站建设谷歌seo综合查询
  • 贵阳做网站方舟网络成人培训班有哪些课程
  • 社保网站上怎么做减员营销网站建设大概费用
  • 网站建设培训教程济宁百度推广电话
  • 怎么做网站浏览量分析360优化大师app
  • wordpress 附件下载统计百度关键词优化系统
  • 企业管理系统网站开发标书网络营销怎么做推广
  • 网站开发常见bug网站代理公司
  • 建设网站的技巧廊坊百度快照优化排名
  • 有哪个网站可以做兼职免费建站
  • 公司建立自己的网站有什么好处青岛seo
  • 怎么在网站后面做链接小程序开发
  • 英文网站建设注意什么怎么做ppt
  • 自己做众筹网站国外免费舆情网站有哪些软件
  • wordpress 登陆函数班级优化大师app
  • 网上网站怎么做广州seo推广公司
  • 四会市城乡规划建设局网站关键词排名优化教程
  • 怎么做网站的动效厦门seo代运营
  • 我的网站打不开最佳搜索引擎磁力
  • 公司的网站费怎样做会计分录上海网站建设关键词排名
  • 好的装修网站夫唯seo视频教程
  • 做建筑设计的网站推荐国际局势最新消息今天
  • seo网站描述seo免费工具
  • 西部数码空间的网站访问统计上海网络seo公司
  • 沈阳网站建设方案报价精准的搜索引擎优化
  • 高端公司网站广告文案经典范例200字
  • 电子商务网站建设计划书市场营销咨询
  • 创新的微商城网站建设百度推广怎么样
  • 哪些网站动效做的不错seo是怎么优化上去