Ejemplo n.º 1
0
class Sender(object):
    """ Use this class to send jobs.
    """
    def __init__(self, identifier, prefix=None, timeout=5.):
        self.identifier = identifier
        self.prefix = prefix or DEFAULT_PREFIX
        self.timeout = timeout

        # Initialize a zeromq context
        self.context = zmq.Context()

        # set up the main controller
        self.soaker = Soaker(MAIN, timeout)

        # Set up a channel to receive results
        self.receiver = Receiver(self.context, self.timeout,
                stream=get_stream(self.prefix, self.identifier, 'receiver'))
        self.receiver.start()

        # Set up a channel to send work
        self.sender = self.context.socket(zmq.PUSH)
        self.sender.bind(get_stream(self.prefix, self.identifier, 'sender'))
        time.sleep(.2)

    def stop(self):
        self.soaker.stop()
        self.receiver.stop()
        time.sleep(.1)
        self.context.destroy(0)

    def execute(self, func_name, data, timeout=5.):
        # create a job ID
        short = binascii.b2a_hex(os.urandom(10))[:10]
        job_id = str(int(time.time())) + short
        job = '%s:%s:%s' % (job_id, func_name, data)

        # callback with the result
        _result = []

        def done(job_id, status, data):
            _result.append((status, data))

        # XXX can we share the event ? I don't think so..
        lock = threading.Event()
        self.receiver.register(done, job_id, lock)
        try:
            self.sender.send(job, zmq.NOBLOCK)
        except zmq.ZMQError, e:
            print(str(e))
            # could not send it
            self.receiver.unregister(done, job_id, lock)
            raise TimeoutError()   # not always timeout

        # waiting for the result
        lock.wait(self.timeout)

        if len(_result) == 0:
            raise TimeoutError()

        return _result[0]
Ejemplo n.º 2
0
    def __init__(self, identifier, prefix=None, timeout=5.):
        self.identifier = identifier
        self.prefix = prefix or DEFAULT_PREFIX
        self.timeout = timeout

        # Initialize a zeromq context
        self.context = zmq.Context()

        # set up the main controller
        self.soaker = Soaker(MAIN, timeout)

        # Set up a channel to receive results
        self.receiver = Receiver(self.context,
                                 self.timeout,
                                 stream=get_stream(self.prefix,
                                                   self.identifier,
                                                   'receiver'))
        self.receiver.start()

        # Set up a channel to send work
        self.sender = self.context.socket(zmq.PUSH)
        self.sender.bind(get_stream(self.prefix, self.identifier, 'sender'))
        time.sleep(.2)
Ejemplo n.º 3
0
    def __init__(self, identifier, prefix=None, timeout=5.):
        self.identifier = identifier
        self.prefix = prefix or DEFAULT_PREFIX
        self.timeout = timeout

        # Initialize a zeromq context
        self.context = zmq.Context()

        # set up the main controller
        self.soaker = Soaker(MAIN, timeout)

        # Set up a channel to receive results
        self.receiver = Receiver(self.context, self.timeout,
                stream=get_stream(self.prefix, self.identifier, 'receiver'))
        self.receiver.start()

        # Set up a channel to send work
        self.sender = self.context.socket(zmq.PUSH)
        self.sender.bind(get_stream(self.prefix, self.identifier, 'sender'))
        time.sleep(.2)
Ejemplo n.º 4
0
class Sender(object):
    """ Use this class to send jobs.
    """
    def __init__(self, identifier, prefix=None, timeout=5.):
        self.identifier = identifier
        self.prefix = prefix or DEFAULT_PREFIX
        self.timeout = timeout

        # Initialize a zeromq context
        self.context = zmq.Context()

        # set up the main controller
        self.soaker = Soaker(MAIN, timeout)

        # Set up a channel to receive results
        self.receiver = Receiver(self.context,
                                 self.timeout,
                                 stream=get_stream(self.prefix,
                                                   self.identifier,
                                                   'receiver'))
        self.receiver.start()

        # Set up a channel to send work
        self.sender = self.context.socket(zmq.PUSH)
        self.sender.bind(get_stream(self.prefix, self.identifier, 'sender'))
        time.sleep(.2)

    def stop(self):
        self.soaker.stop()
        self.receiver.stop()
        time.sleep(.1)
        self.context.destroy(0)

    def execute(self, func_name, data, timeout=5.):
        # create a job ID
        short = binascii.b2a_hex(os.urandom(10))[:10]
        job_id = str(int(time.time())) + short
        job = '%s:%s:%s' % (job_id, func_name, data)

        # callback with the result
        _result = []

        def done(job_id, status, data):
            _result.append((status, data))

        # XXX can we share the event ? I don't think so..
        lock = threading.Event()
        self.receiver.register(done, job_id, lock)
        try:
            self.sender.send(job, zmq.NOBLOCK)
        except zmq.ZMQError, e:
            print(str(e))
            # could not send it
            self.receiver.unregister(done, job_id, lock)
            raise TimeoutError()  # not always timeout

        # waiting for the result
        lock.wait(self.timeout)

        if len(_result) == 0:
            raise TimeoutError()

        return _result[0]