可能であれば、車輪の再発明のフェーズをスキップして、ZeroMQ メッセージング レイヤーを使用してください。
このようにして、コードはすべての低レベルの重要事項を忘れて、開発時間と焦点が問題ドメインの問題に専念する可能性があります。ZeroMQ は、(ほぼ) 線形のスケーラビリティ、リソース プール、障害回復力、上位層の抽象プロトコルなどを追加するための概念と一連の既製のツールの両方を提供します。
最初のケーキとして、ニコラス・ピエルからの眺めを味わってください
Pieters Hintjens の著書「Code Connected, Vol.1」を 1 週間読むと、すぐに使える分散処理の新しい世界が紹介され、The Masters のノウハウを再利用する準備が整います。
あなたは何を得ますか?
数バイトまたは数 GB の BLOB、低レイテンシなど、すべてが次のような単純なコードから、悪魔のように高速に処理されます。
def sendResponse( self, aZmqConnectionToCounterparty, response ):
if response:
try:
aZmqConnectionToCounterparty.send( response, zmq.NOBLOCK )
except ZMQerror:
''' handle ZMQerror '''
except ValueError:
''' handle ValueError '''
except TypeError:
''' handle TypeError '''
...
PyZMQ のドキュメントは次のとおりです。
.send( data, flags = 0, copy = True, track = False )
Send a message on this socket.
This queues the message to be sent by the IO thread at a later time.
Parameters:
-----------
data: object, str, Frame The content of the message.
flags: int Any supported flag: NOBLOCK, SNDMORE.
copy: bool Should the message be sent in a copying or non-copying manner.
track: bool Should the message be tracked for notification
that ZMQ has finished with it? ( ignored if copy = True )
Returns:
--------
None: if copy or not track
None if message was sent, raises an exception otherwise.
MessageTracker: if track and not copy
a MessageTracker object, whose pending property will be True until the send is completed.
Raises:
-------
TypeError If a unicode object is passed
ValueError If track=True, but an untracked Frame is passed.
ZMQError If the send does not succeed for any reason.