是的,ZeroMQ是功能强大的Can-Do工具箱
然而,主要的惊奇将是,ZeroMQ< socket> -s比您在样本中使用的普通的-sockets-结构化得多.
{aZmqContext-> aZmqSocket-> aBehavioralPrimitive}
ZeroMQ在“单个” ZMQ-Context的框架下构建了一个出色的,丰富的抽象框架,ZMQ-Context是(并且将保留)唯一用作“共享”的东西.
线程不应“共享”任何其他“派生”对象,无论它们的状态如何,因为出于简洁设计和高性能及高性能的考虑,已实现了强大的分布式责任框架体系结构.低延迟.
对于所有ZMQ-Socket-,应该宁愿想象一个更加智能的分层子结构,其中的子结构将减轻对I / O活动的担忧(在ZMQ-Context责任内部进行管理-从而保持活动,计时问题公平队列缓冲/选择轮询问题对您来说不再是可见的……),其中一种形式为正式的通信模式行为(由选定的ZMQ-Socket类型原型提供).
最后
ZeroMQ和类似的nanomsg库都是类似LEGO的项目,它们使您成为建筑师和工程师.设计师比起初通常意识到的更多.
因此,人们可以专注于分布式系统的行为,而不是将时间和精力浪费在解决另一种套接字消息[噩梦]上.
(绝对值得一看ZeroMQ的共同父亲Pieter Hintjens的两本书.在这个很棒的主题上,您会发现很多Aha!时刻.)
…以及作为蛋糕上的樱桃-无论是在inproc://上传递某些消息,还是在ipc://上传递某些消息,以及同时在ipc://上传递消息,您都可以在不依赖于传输的通用环境中获得所有这些tcp://图层.
EDIT#12014-08-19 17:00 [UTC 0000]
请检查下面的评论,并进一步审查您的-基本的和高级的-设计选项,以进行"""REQ/REP modified with QUEUE/ROUTER/DEALER add-on ---------------------------
Multithreaded Hello World server
Author: Guillaume Aubert (gaubert)
"""
import time
import threading
import zmq
print "ZeroMQ version sanity-check: ", zmq.__version__
def aWorker_asRoutine( aWorker_URL, aContext = None ):
"""Worker routine"""
#Context to get inherited or create a new one trick------------------------------
aContext = aContext or zmq.Context.instance()
# Socket to talk to dispatcher --------------------------------------------------
socket = aContext.socket( zmq.REP )
socket.connect( aWorker_URL )
while True:
string = socket.recv()
print( "Received request: [ %s ]" % ( string ) )
# do some 'work' -----------------------------------------------------------
time.sleep(1)
#send reply back to client, who asked --------------------------------------
socket.send( b"World" )
def main():
"""Server routine"""
url_worker = "inproc://workers"
url_client = "tcp://*:5555"
# Prepare our context and sockets ------------------------------------------------
aLocalhostCentralContext = zmq.Context.instance()
# Socket to talk to clients ------------------------------------------------------
clients = aLocalhostCentralContext.socket( zmq.ROUTER )
clients.bind( url_client )
# Socket to talk to workers ------------------------------------------------------
workers = aLocalhostCentralContext.socket( zmq.DEALER )
workers.bind( url_worker )
# --------------------------------------------------------------------||||||||||||--
# Launch pool of worker threads --------------< or spin-off by one in OnDemandMODE >
for i in range(5):
thread = threading.Thread( target = aWorker_asRoutine, args = ( url_worker, ) )
thread.start()
zmq.device( zmq.QUEUE, clients, workers )
# ----------------------|||||||||||||||------------------------< a fair practice >--
# We never get here but clean up anyhow
clients.close()
workers.close()
aLocalhostCentralContext.term()
if __name__ == "__main__":
main()