可以在ZeroMQ中实现以下模型吗?

时间:2017-12-30 09:31:08

标签: python sockets zeromq distributed-system pyzmq

Model:

A 理论上“行为”就像服务器一样, D E 都是订阅者和发布者 F 是订阅者。

如果确实可行,可以高度赞赏一个例子。

如果没有,请提供替代方案。 Websockets不是我目标中的一个选项。

1 个答案:

答案 0 :(得分:1)

当然可以:

最佳
首先阅读 [ZeroMQ hierarchy in less than a five seconds] 部分中主要概念差异

为简单起见,我们假设只有 tcp:// 传输类和具有单独IP地址的节点(易于转换为任何共处的情况和/或不同的传输类别)混合)。

对于任何更大的卷和/或严格的延迟管理案例,都必须进行性能调整。

节点-A:

import time, zmq; aCTX = zmq.Context(); aPUB = aCTX.socket( zmq.PUB )
pass;                                   aPUB.bind( "tcp:10.0.0.1:12345" )
pass;                                   aPUB.setsockopt( zmq.CONFLATE, 1 )
pass;                                   print( "A: Started. Can Ctrl+C." )
while True:
      try:
           aPUB.send( "A: sending ...[{0:}]".format( time.ctime() ); time.sleep( 1 )
      except KeyboardInterrupt:
           pass;                        print( "A: Ctrl+C'd. Will terminate" ); break
pass;
aPUB.close()
aCTX.term()

(多个)节点-B(,C):

import time, zmq; aCTX = zmq.Context(); aPUB = aCTX.socket( zmq.PUB )
pass;                                   aPUB.bind( "tcp:10.0.0.10:23456" )
pass;                                   aPUB.setsockopt( zmq.CONFLATE, 1 )
pass;                                   aSUB = aCTX.socket( zmq.SUB )
pass;                                   aSUB.connect( "tcp://10.0.0.1:12345" )
pass;                                   aSUB.setsockopt( zmq.LINGER, 0 )
pass;                                   aSUB.setsockopt( zmq.SUBSCRIBE, "A:" )
pass;                                   print( "B: Started. Can Ctrl+C." )
while True:
      try:
           aPUB.send( "B: sending ...[{0:}]".format( time.ctime() );
           if ( 0 != aSUB.poll(  500, zmq.POLLIN ) ):
               print( "B:recv()'d: {0:} at {1:}".format( aSUB.recv( zmq.NOBLOCK ), time.ctime() )

      except KeyboardInterrupt:
           pass;                        print( "B: Ctrl+C'd. Will terminate" ); break
pass;
aPUB.close()
aSUB.close()
aCTX.term()

节点(一个或多个)-D(,E):

import time, zmq; aCTX = zmq.Context(); aPUB = aCTX.socket( zmq.PUB )
pass;                                   aPUB.bind( "tcp:10.0.0.100:34567" )
pass;                                   aPUB.setsockopt( zmq.CONFLATE, 1 )
pass;                                   aSUB = aCTX.socket( zmq.SUB )
pass;                                   aSUB.connect( "tcp://10.0.0.10:23456" )
pass;                                   aSUB.connect( "tcp://10.0.0.20:23456" )
pass;                                   aSUB.setsockopt( zmq.LINGER, 0 )
pass;                                   aSUB.setsockopt( zmq.SUBSCRIBE, "B:" )
pass;                                   aSUB.setsockopt( zmq.SUBSCRIBE, "C:" )
pass;                                   print( "D: Started. Can Ctrl+C." )
while True:
      try:
           aPUB.send( "D: sending ...[{0:}]".format( time.ctime() )
           if ( 0 != aSUB.poll(  250, zmq.POLLIN ) ):
               print( "D:recv()'d: {0:} at {1:}".format( aSUB.recv( zmq.NOBLOCK ), time.ctime() )
      except KeyboardInterrupt:
           pass;                        print( "D: Ctrl+C'd. Will terminate" ); break
pass;
aPUB.close()
aSUB.close()
aCTX.term()

节点F:

import time, zmq; aCTX = zmq.Context(); aSUB = aCTX.socket( zmq.SUB )
pass;                                   aSUB.connect( "tcp://10.0.0.100:34567" )
pass;                                   aSUB.connect( "tcp://10.0.0.200:34567" )
pass;                                   aSUB.setsockopt( zmq.LINGER, 0 )
pass;                                   aSUB.setsockopt( zmq.SUBSCRIBE, "D:" )
pass;                                   aSUB.setsockopt( zmq.SUBSCRIBE, "E:" )
pass;                                   print( "F: Started. Can Ctrl+C." )
while True:
      try:
           print( "F:recv()'d: {0:} at {1:}".format( aSUB.recv(), time.ctime() )
      except KeyboardInterrupt:
           pass;                        print( "F: Ctrl+C'd. Will terminate" ); break
pass;
aSUB.close()
aCTX.term()