Python消息队列

消息中间件 --->就是消息队列

异步方式:不需要立马得到结果,需要排队

同步方式:需要实时获得数据,坚决不能排队

例子:

#多进程模块multiprocessing

from multiprocessing import Process

from multiprocessing import Queue

def write(q):

for i in ["a", "b", "c", "d"]:

q.put(i)

print ("put {0} to queue".format(i))

def read(q):

while 1:

result = q.get()

print ("get {0} from queue".format(result))

#写一个主函数

def main():

q = Queue()

pw = Process(target=write, args=(q,))

pr = Process(target=read, args=(q,))

pw.start()

pr.start()

pw.join()

#终止pr线程

pr.terminate()

if __name__ == '__main__':

#调用主函数

main()

输出:

put a to queue

put b to queue

put c to queue

put d to queue

多进程模块multiprocessing中pipe方法实现消息队列

例子:

from multiprocessing import Pipe, Process

import time

def proce1(pipe):

for i in xrange(1, 10):

pipe.send(i)

print ("send {0} to pipe".format(i))

time.sleep(1)

def proce2(pipe):

n = 9

while n > 0 :

result = pipe.recv()

print ("recv {0} from pipe".format(result))

def main():

pipe = Pipe(duplex=False)

print (type(pipe))

p1 = Process(target=proce1, args=(pipe[1],))

p2 = Process(target=proce2, args=(pipe[0],))

p1.start()

p2.start()

p1.join()

p2.join()

pipe[0].close()

pipe[1].close()

if __name__ == '__main__':

main()

输出:

<type 'tuple'>

send 1 to pipe

recv 1 from pipe

recv 2 from pipe

send 2 to pipe

recv 3 from pipe

send 3 to pipe

recv 4 from pipe

send 4 to pipe

send 5 to pipe

recv 5 from pipe

recv 6 from pipe

send 6 to pipe

send 7 to pipe

recv 7 from pipe

send 8 to pipe

recv 8 from pipe

send 9 to pipe

recv 9 from pipe

模仿生产者和消费者的多线程消息队列练习

例子:

from threading import Thread

from multiprocessing import Queue

import time

class Proceduer(Thread):

def __init__(self, queue):

super(Proceduer, self).__init__()

self.queue = queue

def run(self):

try:

for i in xrange(1, 10):

print ("put data is {0} to queue".format(i))

self.queue.put(i)

except Exception as e:

print ("put data error")

raise e

class Consumer_odd(Thread):

def __init__(self, queue):

super(Consumer_odd, self).__init__()

self.queue = queue

def run(self):

try:

while not self.queue.empty:

number = self.queue.get()

if number%2 != 0:

print ("get {0} from queue odd. thread name is {1}".format(number, self.getName()))

else:

self.queue.put(number)

time.sleep(1)

except Exception as e:

raise e

class Consumer_even(Thread):

def __init__(self, queue):

super(Consumer_even, self).__init__()

self.queue = queue

def run(self):

try:

while not self.queue.empty:

number = self.queue.get()

if number%2 == 0:

print ("get {0} from queue even.thread name is{1}".format(number, self.getName()))

else:

self.queue.put(number)

time.sleep(1)

except Exception as e:

raise e

def main():

queue = Queue()

p = Proceduer(queue=queue)

p.start()

p.join()

time.sleep(1)

c1 = Consumer_odd(queue=queue)

c2 = Consumer_even(queue=queue)

c1.start()

c2.start()

c1.join()

c2.join()

print ("ALL thread terminate")

if __name__ == '__main__':

main()

时间: 2024-07-31 07:06:50

Python消息队列的相关文章

python消息队列snakemq使用总结

Python 消息队列snakemq总结 最近学习消息总线zeromq,在网上搜了python实现的消息总线模块,意外发现有个消息队列snakemq,于是拿来研究一下,感觉还是很不错的,入手简单使用也简单(比ice强多了),就是资料太少了,只能自己抠. 一.关于snakemq的官方介绍 1纯python实现,跨平台 2自动重连接 3可靠发送--可配置的消息方式与消息超时方式 4持久化/临时 两种队列 5支持异步 -- poll() 6symmetrical -- 单个TCP连接可用于双工通讯 7

Python消息队列(RabbitMQ)

RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用.可维护多个队列,可实现消息的一对一和广播等方式发送 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.ActionScript.XMPP.STOMP等,支持AJAX.用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗. CentOs安装: 安装socat yum

python消息队列Queue

实例1:消息队列Queue,不要将文件命名为"queue.py",否则会报异常"ImportError: cannot import name 'Queue'" #coding=utf-8 from multiprocessing import Queue  q = Queue(3)#初始化一个Queue对象,最多可接收三条put消息 q.put('message-1') q.put('message-2') print(q.full())#False,是否满了 q

python 消息队列-rabbitMQ 和 redis介绍使用

1.rabbitMQ 与ptyhon 进程queue 区别.进程queue 主要用户Python父子进程之间或者统一进程不同子进程.rabbit可以用户不同语言之前的相互交流,socket可以实现同样功能,但是较为复杂. 2. rabbitMQ  消息轮训.一个生产者对多个消费者时候.会自动将消息轮训给不同消费者. # Author : xiajinqi import pika connetction = pika.BlockingConnection(pika.ConnectionParame

python之消息队列

引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松地解决这些问题.消息服务擅长于解决多系统.异构系统间的数据交换(消息通知/通讯)问题,你也可以把它用于系统间服务的相互调用(RPC).本文将要介绍的RabbitMQ就是当前最主流的消息中间件之一. RabbitMQ简介 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源

Python并发编程-RabbitMq消息队列

消息中间件 --->就是消息队列 异步方式:不需要立马得到结果,需要排队 同步方式:需要实时获得数据,坚决不能排队 subprocess 的Q也提供不同进程之间的沟通 应用场景: 买票,抢购 堡垒机批量发送文件 Centos6.x系统编译安装RabbitMQ 一.系统环境 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [[email protected] ~]# uname -r 2.6.32-

自动化运维Python系列之消息队列RabbitMQ

RabbitMQ RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多公开标准(如 COBAR的 IIOP ,或者是 SOAP 等),但是在异步消息处理中却不是这样,只有大企业有一些商业实现(如微软的 MSMQ ,IBM 的 Websphere MQ 等),因此,在 2006 年的 6 月,Cisco .Redhat.iMatix 等联合制定了 AMQP 的

python 操作消息队列

图示 其中P指producer,即生产者:C指consumer,即消费者.中间的红色表示消息队列,实例中表现为HELLO队列. 往队列里插入数据前,查看消息队列 $sudo rabbitmqctl list_queues Listing queues ... celeryev.db53a5e0-1e6a-4f06-a9f7-2c104c4612fb 0 ...done. 插入消息队列代码 #in_queue.py #coding=utf8 import pika connection = pik

python中消息队列RabbitMQ的使用

1,简介 RabbitMQ(Rabbit Message Queue)是流行的开源消息队列系统,用erlang语言开发. 1.1关键词说明: Broker:消息队列服务器实体.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.Queue:消息队列载体,每个消息都会被投入到一个或多个队列.Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来.Routing Key:路由关键字,exchange根据这个关键字进行消息投递.vhost:虚拟主机,一个b