在python中自己就是存在队列queue。一个是线程队列queue,另外一个是进程multiprocessing中的队列Queue。html
线程queue:只用于线程之间的数据交互python
进程Queue:用于同一进程下父进程和子进程之间的数据交互,或者同属于一个父进程下的多个子进程之间的交互linux
若是是多个进程间(不一样的应用程序之间)、多个系统须要进行数据交互,那么就能够使用消息服务来解决这些问题。消息服务擅长于解决多系统、异构系统间的数据交换(消息通知/通信)问题,你也能够把它用于系统间服务的相互调用(RPC)。RabbitMQ就是当前最主流的消息中间件之一。windows
RabbitMQ的基础介绍:点击查看函数
实现最简单的队列通讯线程
send端 3d
# -*- coding: UTF-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) # 声明一个管道 channel = connection.channel() # 声明queue channel.queue_declare(queue='hello') # queue的名称 # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print(" [x] Sent 'Hello World!'") connection.close()
receive端 code
# -*- coding: UTF-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = connection.channel() # You may ask why we declare the queue again ‒ we have already declared it in our previous code. # We could avoid that if we were sure that the queue already exists. For example if send.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. # 这个queue名称就是刚才send端中声明的,再次声明就能保证找到生产者发送的数据 # 若是消费者在生产者以前先启动了,会找不到这个消息队列,就会报错 channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print('--->>', ch, '\n', method, '\n', properties) print(" [x] Received %r" % body) # ch: 声明的管道channel对象内存地址 # channel.basic_consume(callback, # 若是收到消息就调用callback函数来处理消息 queue='hello', no_ack=True) print(' [*] Waiting for messages. To exit press CTRL+C') # 永远收下去,没有就在这卡住 channel.start_consuming()
链接远程rabbitmq可能会有认证问题,须要输入用户名和密码server
windows下设置用户名、密码和权限:herehtm
linux环境下设置用户名、密码和权限:here
基本都在最后面
send端的设置:
# -*- conding:utf-8 -*- import pika # 认证信息 credentials = pika.PlainCredentials('bigberg', '111111') connection = pika.BlockingConnection(pika.ConnectionParameters( '172.16.200.109', 5672, '/', credentials)) # 声明一个管道 channel = connection.channel() # 声明queue channel.queue_declare(queue='hello') # queue的名称 # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. channel.basic_publish(exchange='', routing_key='hello', body='from remote server') print(" [x] Sent 'from remote server'") connection.close()