python zeromq


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

hwserver.py

Python代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  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代碼 復制代碼 收藏代碼
  1. import zmq
  2. context = zmq.Context()
  3. socket = context.socket(zmq.PAIR)
  4. socket.bind("tcp://127.0.0.1:5555")

client

Python代碼 復制代碼 收藏代碼
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM