KZKY memo

自分用メモ.

Python: ZeroMQ2

基本パターン

  • req/res: server/client (server-client blocking, a client can connect to many servers)
  • pub/sub: broadcast (non reliable publish)
  • push/pull: loadbalancing

は以前やった
kzky.hatenablog.com
ので,

Intermediaries and Proxiesを学べば,大体の基本パターンは網羅したことになる.
Intermediaries and Proxiesとは,

The messaging industry calls this intermediation, meaning that the stuff in the middle deals with either side. In ZeroMQ, we call these proxies, queues, forwarders, device, or brokers, depending on the context.

というように,message brokerとかproxyのような仲介者のこと.
これらは,zeromqではdevicesなんて呼ばれたりする.

ここではDeviceが用意されている3パターンをまとめる.

The Dynamic Discovery Problem

PUB/XSUB/XPUB/SUBのメッセージングパターン

どうやって追加されたノードを動的に見つけるか?
一番簡単なのは,ハードコード(再設定)すること.ハードコードだと,ノード動的に追加されたら,コードを変えて,プロセスを起動する.publisher-to-subscriberが1-to-nなら,subscriberを起動すればいいが,publisher-to-subscriberがn-to-mの場合はそう簡単にはいなかない.なので,proxyを間に挟むPUB/XSUB/XPUB/SUBのメッセージングパターンを使う.


f:id:KZKY:20151215222536p:plain

f:id:KZKY:20151215222539p:plain

My Code Samples

一番簡単な,zmq.FORWARDERを使っている.

XSUB/XPUBの間にコードを挟む場合はpoller.poll()を使う

実行例

各コマンドを別terminalで実行

python forwarder_device.py

python forwarder_subscriber.py
python forwarder_publisher.py
python forwarder_subscriber.py
python forwarder_publisher.py
...

subscriber, publisherの動的な追加および,broadcastされていることを確認する.

Shared Queue (DEALER and ROUTER sockets)

REQ/ROUTER/DEALER/REPのメッセージングパターン

The Dynamic Discovery Problemと似たような問題がある.
serverが3つあって,clientが1つの場合を考える.新しくクライアントを追加しようとする場合に,clientは既存のサーバトポロジーを知っているので,3つのserversを指定すればいい.負荷がもっとかかって,serverを追加したい場合はどうするか?どうやってクライアントは,新しく追加したいサーバを発見する? サービスのペイロードがあまりない夜中に,この作業したくないよね? サーバーのトポロジーを知っているブローカーを置けば,この問題は解決する.

f:id:KZKY:20151215222538p:plain

f:id:KZKY:20151215222537p:plain

My Code Samples

実行例

各コマンドを別terminalで実行

python queue_device.py
...
python queue_server.py
python queue_client.py
...
python queue_server.py
python queue_client.py

deviceは先に立ち上げておくこと.ロードバランシングおよび,server, clientの動的な追加ができていることを確認する.この例はAsyncでなくて,Syncなので注意.
Queue Deviceは,完全なSyncと考えて良い.

自分でもっとコントロールしたり,何かしらコントロールをしたい場合は,poller.pollを使う.

Streamer

PUSH/STREAMER/PULLのメッセージングパターン

f:id:KZKY:20151215222540p:plain

この例はSyncでなくて,Ayncなので注意.push/pullの間にstreamerが入るのが,fire-and-forgetと捉えても良いと思う.

My Code Samples

実行例

各コマンドを別terminalで実行

python streamder_device.py 

python streamder_server.py
python streamder_server.py
python streamder_client.py

余談

やっぱりミドルウェアではない.柔軟に分散P2Pプログラミングを行いたいとか,自分でMWを作りたい時のlibrary (middle-level API)として使うとかがいい.ただ,メッセージパターンに対するhow-toは用意されているので,python socket moduleを使うよりは楽だと思う.

http://zguide.zeromq.org/page:all#Audience=title:Audience

This book is written for professional programmers who want to learn how to make the massively distributed software that will dominate the future of computing. We assume you can read C code, because most of the examples here are in C even though ZeroMQ is used in many languages. We assume you care about scale, because ZeroMQ solves that problem above all others. We assume you need the best possible results with the least possible cost, because otherwise you won't appreciate the trade-offs that ZeroMQ makes. Other than that basic background, we try to present all the concepts in networking and distributed computing you will need to use ZeroMQ.