简体   繁体   中英

Publishing messages from Twisted Client to ZeroMQ

In my Twisted application , i want to publish messages to the ZeroMQ message queue. Can i do something like below in the Protocol class ?

from twisted.internet import protocol,reactor
import zmq

class MyClient(protocol.Protocol):
  def __init__(self):
    self.context = zmq.Context()
    self.socket =  context.socket(zmq.PUB)
    self.socket.bind("tcp://127.0.0.1:5000")

  def dataReceived(self,data):
     #Do something with the data to get a result
     #...
     #Publish to 0mq
     self.socket.send(result)

#Code below for factory and initializing reactor
#...

reactor.run()

Would the above work, if not why ?. If it wont work then should I use txZMQ ( https://github.com/smira/txZMQ ) ?

Thanks

The default behaviour of the Socket.send method is to block until the message has been queued on the socket. If the socket queue is full, then nothing else will happen in your Twisted application until there is room on the socket to queue the message.

The NOBLOCK flag to the send method tells zmq to raise an exception if it can't queue the message. But if you do this, then you have to worry about failures:

try:
    self.socket.send(result, flags=zmq.NOBLOCK)
except zmq.Again:
    # Failed to queue the message: what now?

It would simpler to use the txZMQ package, which integrates ZMQ with the Twisted event loop. The examples in the documentation suggest that you implement publishing in txZMQ like this:

factory = ZmqFactory()
publisher = ZmqPubConnection(factory, endpoint)
publisher.publish(message)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM