def test_send_recv(self): """ Check hsending and receiving """ def onListen(pusher, puller, d): # now connect the puller dpuller = puller.connect(self.factory) dpuller.addCallback(onConnect, pusher, d) def onConnect(puller, pusher, d): reactor.callLater(0.2, perform_test, pusher, puller, d) def perform_test(pusher, puller, d): for i in range(0,5): pusher.push(str(i)) reactor.callLater(0.2, check, pusher, puller, d) def check(pusher, puller, d): result = getattr(puller, 'messages', []) expected = [ ['0'], ['1'], ['2'], ['3'], ['4'] ] self.failUnlessEqual( result, expected, "Message should have been received") d.callback(True) pusher = ZmqPushConnection(ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5858")) puller = ZmqTestPullConnection(ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5858")) d = defer.Deferred() dpush = pusher.listen(self.factory) dpush.addCallback(onListen, puller, d) return d
class Worker(object): def __init__(self): self.factory = None self.receiver = None self.sink = None def start(self): self.factory = ZmqFactory() endpoint = ZmqEndpoint(ZmqEndpointType.connect, "tcp://localhost:5557") self.receiver = ZmqPullConnection(endpoint) self.receiver.messageReceived = self.performWork deferred = self.receiver.connect(self.factory) deferred.addCallback(self.onReceiverConnected) endpoint = ZmqEndpoint(ZmqEndpointType.connect, "tcp://localhost:5558") self.sink = ZmqPushConnection(endpoint) deferred = self.sink.listen(self.factory) deferred.addCallback(self.onSinkConnected) def stop(self): self.factory.shutdown() def onReceiverConnected(self, receiver): print "worker connected to task ventilator" def onSinkConnected(self, sink): print "worker connected to task sink" def performWork(self, message): # send results to sink after waiting specified job time # in this example only single-part messages are used time_interval = message[0] time_wait = int(time_interval) * 0.001 reactor.callLater(time_wait, self.sendWorkResult, "") def sendWorkResult(self, result): self.sink.send(result)
def start(self): self.factory = ZmqFactory() endpoint = ZmqEndpoint(ZmqEndpointType.connect, "tcp://localhost:5557") self.receiver = ZmqPullConnection(endpoint) self.receiver.messageReceived = self.performWork deferred = self.receiver.connect(self.factory) deferred.addCallback(self.onReceiverConnected) endpoint = ZmqEndpoint(ZmqEndpointType.connect, "tcp://localhost:5558") self.sink = ZmqPushConnection(endpoint) deferred = self.sink.listen(self.factory) deferred.addCallback(self.onSinkConnected)
from tx0mq import constants, ZmqEndpoint, ZmqEndpointType, ZmqFactory, ZmqPushConnection parser = OptionParser("") parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect") parser.add_option("-e", "--endpoint", dest="endpoint", default="ipc:///tmp/sock", help="0MQ Endpoint") # parser.set_defaults(method="connect", endpoint="ipc:///tmp/txzmq-pc-demo") if __name__ == "__main__": (options, args) = parser.parse_args() def onConnect(pusher): print "pusher connected" # discard unsent messages on close pusher.setSocketOptions({constants.LINGER: 0, constants.HWM: 1}) produce(pusher) def produce(pusher): data = str(time.time()) print "pushing %s ..." % (data) pusher.push(data) reactor.callLater(1, produce, pusher) endpoint = ZmqEndpoint(ZmqEndpointType.connect, options.endpoint) pusher = ZmqPushConnection(endpoint) deferred = pusher.listen(ZmqFactory()) deferred.addCallback(onConnect) reactor.run()