python - Why is this zmq code not working? -
this test doesn't work.
class printhandler(messagehandler): def handle_message(self, message): print(message) class filehandler(messagehandler): def handle_message(self, message): open('nana', 'w') f: f.write(message) class subscribeprocess(process): def __init__(self, handler): super(subscribeprocess, self).__init__(group=none, target=none, name=none, args=(), kwargs={}) self.handler = handler def run(self): self.address = tcpaddress(host='127.0.0.1', port=5555) subscriber = zmqsubscriber(zmqblockingconnection(address=self.address, bind=false)) subscriber.set_message_handler(self.handler) print('............') class testzmqsubscriber(testcase): def test_set_message_handler(self): address = tcpaddress(host='127.0.0.1', port=5555) pub_connection = zmqblockingconnection(address, bind=true) publisher = zmqpublisher(pub_connection) p = subscribeprocess(handler=printhandler()) p.start() while true: publisher.publish('message number {}'.format(2))
i that's not unit test actually. want see received messages in console first. write proper test.
while 2 scripts work perfectly.
connection = zmqblockingconnection(tcpaddress(host='127.0.0.1', port=5555), bind=false) sub = zmqsubscriber(connection) sub.set_message_handler(printhandler())
address = tcpaddress(host='127.0.0.1', port=5555) pub_connection = zmqblockingconnection(address, bind=true) publisher = zmqpublisher(pub_connection) while true: publisher.publish('message number {}'.format(2))
inside of subscriber.set_message_handler(handler) this
def start_receiving_messages(self, message_handler): while true: message_handler.handle_message(self.socket.recv())
and in debugger see code hangs infinitely in socket.recv()
maybe i'm using multiprocessing wrong?
edit1
class zmqblockingconnection(connection): def start_receiving_messages(self, message_handler): while true: message_handler.handle_message(self.socket.recv()) def send_message(self, message): self.socket.send(message) def __init__(self, address, bind, hwm=1000): self.hwm = hwm self.bind = bind self.address = address self.socket = none def set_hwm(self, hwm): self.socket.set_hwm(hwm) def configure(self, socket_type): self.socket = zmq.context().socket(socket_type) if self.bind: self.socket.bind(str(self.address)) else: self.socket.connect(str(self.address)) self.set_hwm(self.hwm)
ok, problem in
def configure(self, socket_type): self.socket = zmq.context().instance().socket(socket_type) if self.bind: self.socket.bind(str(self.address)) else: self.socket.connect(str(self.address)) self.set_hwm(self.hwm)
so instead of using singleton started create context instances , it's working.
Comments
Post a Comment