Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
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)