python,zeromq和pub / sub - remote sub没有获取数据

时间:2013-12-30 05:21:24

标签: python zeromq

在我的发布商处,我使用以下内容发送数据。

context_forward2 = zmq.Context()
forward2_socket = context_forward2.socket(zmq.PUB)
forward2_socket.bind("tcp://127.0.0.1:7002")
transpport = 'orderbook stuff'
forward2_socket.send(transpport)

在我的订阅者服务器上......

context = zmq.Context()
forward_socket = context.socket(zmq.SUB)
forward_socket.connect("tcp://89.55.55.55:7002")
forward_socket.setsockopt(zmq.SUBSCRIBE, 'orderbook')
time.sleep(5)
while True:
    print 'a'
    transpport = forward_socket.recv()
    print 'b'
    transpport = transpport.split('orderbook ')[1]
    print 'c'
    print transpport

但问题是它只是挂在transpport = forward_socket.recv()

为什么zeromq不起作用?

2 个答案:

答案 0 :(得分:2)

如果您希望外部世界连接。您需要绑定89.55.55.550.0.0.0(但不是127.0.0.1,它只监听本地tcp包)

答案 1 :(得分:0)

在发布商time.sleep(5)之前添加socket.send()。在传输数据之前,您需要在发布者和订阅者之间建立连接。您的问题是时间问题。

以下程序对我有效:

出版商:

import zmq 
import time
context_forward2 = zmq.Context()
forward2_socket = context_forward2.socket(zmq.PUB)
forward2_socket.bind("tcp://127.0.0.1:7002")
transpport = 'orderbook stuff'
time.sleep(5)
forward2_socket.send(transpport)

订户:

import zmq 
import time
context = zmq.Context()
forward_socket = context.socket(zmq.SUB)
forward_socket.connect("tcp://127.0.0.1:7002")
forward_socket.setsockopt(zmq.SUBSCRIBE, 'orderbook')
time.sleep(5)
while True:
  print 'a' 
  transpport = forward_socket.recv()
  print transpport
  print 'b' 
  transpport = transpport.split('orderbook ')[1]
  print 'c' 
  print transpport

(您的静态IP地址已替换为127.0.0.1,因为我正在本地计算机上进行测试)