python zeromq

ZeroMQ消息层实践
本文介绍了如何使用ZeroMQ实现消息层,包括选择传输协议、建立基础及选择通讯模式等步骤。并通过实例展示了Request/Reply模式下的负载均衡、Pub/Sub模式下的广播及监听,以及Pair模式下的点对点通讯。

python examples https://github.com/imatix/zguide/tree/master/examples/Python

hwserver.py

Python代码 复制代码 收藏代码spinner.gif
  1. #
  2. # Hello World server in Python
  3. # Binds REP socket to tcp://*:5555
  4. # Expects "Hello" from client, replies with "World"
  5. #
  6. import zmq
  7. import time
  8. context = zmq.Context()
  9. socket = context.socket(zmq.REP)
  10. socket.bind("tcp://*:5555")
  11. while True:
  12. # Wait for next request from client
  13. message = socket.recv()
  14. print "Received request: ", message
  15. # Do some 'work'
  16. time.sleep (1) # Do some 'work'
  17. # Send reply back to client
  18. socket.send("World")

hwclient.py

Python代码 复制代码 收藏代码spinner.gif
  1. #
  2. # Hello World client in Python
  3. # Connects REQ socket to tcp://localhost:5555
  4. # Sends "Hello" to server, expects "World" back
  5. #
  6. import zmq
  7. context = zmq.Context()
  8. # Socket to talk to server
  9. print "Connecting to hello world server..."
  10. socket = context.socket(zmq.REQ)
  11. socket.connect ("tcp://localhost:5555")
  12. # Do 10 requests, waiting each time for a response
  13. for request in range (1,10):
  14. print "Sending request ", request,"..."
  15. socket.send ("Hello")
  16. # Get the reply.
  17. message = socket.recv()
  18. print "Received reply ", request, "[", message, "]"

问题3:zeroMQ实现一个消息层?

答:

实现一个ZeroMQ消息层需要三个步骤:

1.选择传输协议

0MQ提供了4种不同的传输协议

INPROC an In-Process communication model

IPC an Inter-Process communication model

MULTICAST multicast via PGM, possibly encapsulated in UDP

TCP a network based transport

2.建立基础

由于在网络中两个端点是相对动态的,很难有一个稳定的单一连接点。

如果是这种情况,可以使用由0MQ提供的转发设备。

转发设备可以绑定2个不同端口,并且转发消息从一个端点到另一个端点。

这样做的话,在网络中转发设备能够变成一个稳定的点,其它组件都可以去连接。

0MQ提供了3种类型的设备

QUEUE, a forwarder for the request/response messaging pattern

FORWARDER, a forwarder for the publish/subscribe messaging pattern

STREAMER, a forwarder for the pipelined messaging pattern

3.选择通讯模式

0MQ支持4种模式

REQUEST/REPLY, bidirectional, load balanced and state based

PUBLISH/SUBSCRIBE, publish to multiple recipients at once

UPSTREAM / DOWNSTREAM, distribute data to nodes arranged in a pipeline

PAIR, communication exclusively between peers

Req/Rep

均衡负载请求:

server 1

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.REP)
  4. socket.bind("tcp://127.0.0.1:5000")
  5. while True:
  6. msg = socket.recv()
  7. print "Got", msg
  8. socket.send(msg)

server 2

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.REP)
  4. socket.bind("tcp://127.0.0.1:6000")
  5. while True:
  6. msg = socket.recv()
  7. print "Got", msg
  8. socket.send(msg)

client

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.REQ)
  4. socket.connect("tcp://127.0.0.1:5000")
  5. socket.connect("tcp://127.0.0.1:6000")
  6. for i in range(10):
  7. msg = "msg %s" % i
  8. socket.send(msg)
  9. print "Sending", msg
  10. msg_in = socket.recv()

会发现client的请求会被均衡的分配给两个server

Example client output:

Sending msg 0
Sending msg 1
Sending msg 2
Sending msg 3
Sending msg 4
Sending msg 5
Sending msg 6
Sending msg 7
Sending msg 8
Sending msg 9

Example output server 1 at port 5000:

Got msg 0
Got msg 2
Got msg 4
Got msg 6
Got msg 8

Example output server 2 at port 6000:

Got msg 1
Got msg 3
Got msg 5
Got msg 7
Got msg 9

现在,如果我们要加入一个额外的server去管理我们的请求,我们将不得不修改我们的代码。

这是非常麻烦的,我们需要让每个client都知道有一个额外的server可以均衡请求。

为了解决这个问题,替代client直接去连接多个server的方式,client去连接转发设备,再由转发设备路由全部的消息给server。

Pub/Sub

在pub/sub模式下组件是松耦合的。类似于广播电台。

一个广播server为现场足球赛

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. from random import choice
  3. context = zmq.Context()
  4. socket = context.socket(zmq.PUB)
  5. socket.bind("tcp://127.0.0.1:5000")
  6. countries = ['netherlands','brazil','germany','portugal']
  7. events = ['yellow card', 'red card', 'goal', 'corner', 'foul']
  8. while True:
  9. msg = choice( countries ) +" "+ choice( events )
  10. print "->",msg
  11. socket.send( msg )<span style="white-space: normal;"> </span>

输出

-> portugal corner
-> portugal yellow card
-> portugal goal
-> netherlands yellow card
-> germany yellow card
-> brazil yellow card
-> portugal goal
-> germany corner

一个客户端去收听特定的消息

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.SUB)
  4. socket.connect("tcp://127.0.0.1:5000")
  5. socket.setsockopt(zmq.SUBSCRIBE, "netherlands")
  6. socket.setsockopt(zmq.SUBSCRIBE, "germany")
  7. while True:
  8. print socket.recv()

输出

netherlands red card
netherlands goal
netherlands red card
germany foul
netherlands yellow card
germany foul
netherlands goal
netherlands corner
germany foul
netherlands corner

Pipelining

并发处理数据,其工作模式

一个工作者得到来自上游socket的消息,一旦处理完成后发送消息到下游。

Paired socket

服务器监听某个端口,客户端连接到这个端口,消息可以双向流动。

server

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.PAIR)
  4. socket.bind("tcp://127.0.0.1:5555")

client

Python代码 复制代码 收藏代码spinner.gif
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.PAIR)
  4. socket.connect("tcp://127.0.0.1:5555")
 
 

ps:

推荐

http://www.zeromq.org/

http://nichol.as/zeromq-an-introduction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值