rabbitmq

解释器:D:\360Downloads\py
线程队列 同一个进程中不同线程的交互;
进程队列 不同进程的交互;
rabbitmq 不同机器不同语言之间的交互,此时使用消息中间件即消息队列(异步);
开发语言是:erlang

python使用pika模块
windows安装https://www.rabbitmq.com/download.html

easy_install pika

配置环境
名称ERLANG_HOME,值为本机中erlang的安装目录。
再在用户变量PATH中添加上%ERLANG_HOME%\bin;
设置环境变量RABBITMQ_SERVER安装目录。
添加到PATH %RABBITMQ_SERVER%\sbin;
检测安装成功

erl -version
rabbitmq-service

rabbitmq支持多个系统同时使用,可同时创建多个独立的队列,根据队列名区分。机制为:消息发送给交换机(exchange),交换机再将消息放入各自独立的队列中。

python使用rabbitmq,rec接收send消息进行处理,从队列中取出,不回复完成确认。

在terminal终端下打开多个框
一个输入 python receiver1.py 表示接收
一个输入 python receiver1.py 表示接收
另一个输入python sender1.py表示发送数据

sender1.py

import pika
'''
# 队列名和密码
credentials = pika.PlainCredentials('root', 'root123')
# 连接机器
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '*.*.*.*',credentials=credentials))
# 建立rabbit协议的通道
channel = connection.channel()
'''
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel( )
# 声明queue
channel.queue_declare(queue='hello')
# send RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
# routing_key 接收的队列名 body接收的消息
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

receiver1.py

import pika
import time
'''
# 队列名和密码
credentials = pika.PlainCredentials('root', 'root123')
# 连接机器
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '*.*.*.*',credentials=credentials))
# 建立rabbit协议的通道
channel = connection.channel()
'''
# why we declare the queue again ‒ we have already declared it in our previous code.sender.py
# We could avoid that if we were sure that the queue already exists. For example if sender1.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
connection = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
channel.basic_consume('hello',callback,True)# 拿到消息找callback # True不需要回复确认
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming() # 开始消费,没有内容就一直等待,死循环

rec接收send消息进行处理,回复完成确认,若未收到,则转发给别的消费者处理

在terminal终端下打开多个框
一个输入 python receiver.py 表示接收
一个输入 python receiver.py 表示接收
另一个输入python sender.py表示发送数据

sender

import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel( )
# 声明queue队列持久化,重启后仍存在
channel.queue_declare(queue='task_queue',durable=True)
# send RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
import sys
message = ' '.join(sys.argv[1:]) or "Hello World! %s" % time.time()
channel.basic_publish(exchange='',
                      routing_key='task_queue',
                      body=message,
                      properties=pika.BasicProperties(
                      delivery_mode=2,  # 消息持久化,重启后存在
                      )
                      )
print(" [x] Sent %r" % message)
connection.close()

receiver

import pika, time
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel( )
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    time.sleep(20)
    print(" [x] Done")
    print("method.delivery_tag", method.delivery_tag)
    ch.basic_ack(delivery_tag=method.delivery_tag)# 回复完成确认 返回标识符
channel.basic_consume('task_queue',callback,False) # False 需要回复确认
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

一对多情况–发布订阅模式

每个订阅者一个队列,且消息实时。exchange的作用:根据订阅列表同时转发消息给所有订阅者。
fanout: 所有bind到此exchange的queue都可以接收消息
direct: 通过routingKey和exchange决定某个唯一的queue可以接收消息
topic:所有符合routingKey(此时可以是一个表达式)的routingKey所bind的queue可以接收消息
headers: 通过headers 来决定把消息发给哪些queue

fanout:实时订阅 广播

在terminal终端下打开多个框
一个输入 python fanout_receive.py 表示接收
一个输入 python fanout_receive.py 表示接收
另一个输入python fanout_send.py表示发送数据

fanout_send

import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',exchange_type='fanout')
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
#设置类型为fanout后,不需添加routing_key
channel.basic_publish(exchange='logs',
                      routing_key='',
                      body=message)
print(" [x] Sent %r" % message)
connection.close()

fanout_receive

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
# 不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
result = channel.queue_declare('',exclusive=True)
queue_name = result.method.queue
# 将队列绑定到交换机上收消息。
channel.queue_bind(exchange='logs', queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
    print(" [x] %r" % body)
channel.basic_consume(queue_name,callback,True)
channel.start_consuming()

direct: 组播

在terminal终端下打开多个框
一个输入 python direct_receive.py info 表示接收info数据
一个输入 python direct_receive.py info error 表示接收info error数据
另一个输入python direct_send.py error python direct_send.py info表示发送数据

direct_send

import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',exchange_type='direct')
# 设置严重程度,级别
severity = sys.argv[1] if len(sys.argv)> 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'

channel.basic_publish(exchange='direct_logs',
                      routing_key=severity,
                      body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()

direct_receive

import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',exchange_type='direct')
result = channel.queue_declare('',exclusive=True)
queue_name = result.method.queue
severities = sys.argv[1:]
# severities为空
if not severities:
    sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
    sys.exit(1)
# 绑定级别
for severity in severities:
    channel.queue_bind(exchange='direct_logs',queue=queue_name,routing_key=severity)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(queue_name,callback,True)
channel.start_consuming()

topic:更细致划分

To receive all the logs run:python receive_logs_topic.py "#"
To receive all logs from the facility “kern”:python receive_logs_topic.py "kern.*"
Or if you want to hear only about “critical” logs:python receive_logs_topic.py "*.critical"
You can create multiple bindings:python receive_logs_topic.py "kern.*" "*.critical"

在terminal终端下打开多个框
一个输入 python topic_receive.py "#" 表示接收所有数据
一个输入 python topic_receive.py "mysql.*" 表示接收mysql的数据
另一个输入python topic_send.py mysql.info hello python topic_send.py mys.info hello 表示发送数据

topic_send

import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',exchange_type='topic')
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs',routing_key=routing_key,body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()

topic_receive

import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',exchange_type='topic')
result = channel.queue_declare('',exclusive=True)
queue_name = result.method.queue
binding_keys = sys.argv[1:]
if not binding_keys:
    sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
    sys.exit(1)
for binding_key in binding_keys:
    channel.queue_bind(exchange='topic_logs',
                       queue=queue_name,
                       routing_key=binding_key)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
channel.basic_consume(queue_name,callback,True)
channel.start_consuming()

双向通信

先启动server端 server.py
python server.py

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()
channel.queue_declare(queue='rpc_queue')

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)
def on_request(ch, method, props, body):
    n = int(body)
    print(" [.] fib(%s)" % n)
    response = fib(n)
    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id=props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)
# 本来是公平分发,但是加上这句会保证在非空闲时不被再发消息
channel.basic_qos(prefetch_count=1)
channel.basic_consume('rpc_queue',on_request,False)
print(" [x] Awaiting RPC requests")
channel.start_consuming()

client端 client.py
python client.py

import pika
import uuid
class FibonacciRpcClient(object):
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            'localhost'))
        self.channel = self.connection.channel()
        result = self.channel.queue_declare('',exclusive=True)
        self.callback_queue = result.method.queue
        self.channel.basic_consume(self.callback_queue,self.on_response,True) # 准备接受命令,调用callback函数
    def on_response(self, ch, method, props, body):
        # callback方法 检查唯一标识符
        if self.corr_id == props.correlation_id:
            self.response = body
    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4()) # uuid 唯一标识符
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                   reply_to=self.callback_queue,# 将结果发送给reply_to
                                   correlation_id=self.corr_id,
                                   ),
                                   body=str(n))
        # count  = 0
        while self.response is None:
            self.connection.process_data_events() # 检查队列里有没有新消息,但不会阻塞
            # count +=1
            # print("check...",count)
        # 返回输出值
        return int(self.response)
fibonacci_rpc = FibonacciRpcClient()
print(" [x] Requesting fib(30)")
response = fibonacci_rpc.call(30)
print(" [.] Got %r" % response)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值