Exemple #1
0
    def subscribe_producer(
            request_queue_name: str, answer_queue_name: str,
            request_callback: Callable[[dict, Callable[[any], None]], bool]):
        def answer_callback(msg_id: int, answer) -> None:
            total_answer = {
                'id': msg_id,
                'ans': answer,
            }
            QueueWrapper.send_message(answer_queue_name,
                                      dump_object(total_answer))

        def req_callback(ch: BlockingChannel, method: spec.Basic.Deliver,
                         _: spec.BasicProperties, body: bytes):
            body: ReqBody = load_object(body)
            ack = request_callback(
                body['req'],
                lambda answer: answer_callback(body['id'], answer))
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        QueueWrapper.subscribe_to_queue(callback=req_callback,
                                        queue=request_queue_name,
                                        auto_ack=False)
    def subscribe_getter(queue_name, callback):
        def req_callback(ch, method, properties, body):
            body = load_object(body)
            ack = callback(body)
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        QueueWrapper.subscribe_to_queue(callback=req_callback,
                                        queue=queue_name,
                                        no_ack=False)
Exemple #3
0
    def subscribe_getter(queue_name, callback):
        def req_callback(ch: BlockingChannel, method: spec.Basic.Deliver,
                         _: spec.BasicProperties, body: bytes):
            body = load_object(body)
            ack = callback(body)
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        QueueWrapper.subscribe_to_queue(callback=req_callback,
                                        queue=queue_name,
                                        auto_ack=False)
    def get_consumer(request_queue_name, answer_queue_name, answer_callback):
        def raw_answer_callback(ch, method, properties, body):
            body = load_object(body)
            ack = answer_callback(body['id'], body['ans'])
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        def write_msg(msg_id, request):
            message = {'id': msg_id, 'req': request}
            packed = dump_object(message)
            QueueWrapper.send_message(request_queue_name, packed)

        QueueWrapper.subscribe_to_queue(callback=raw_answer_callback,
                                        queue=answer_queue_name,
                                        no_ack=False)
        return write_msg
    def subscribe_producer(request_queue_name, answer_queue_name, request_callback):
        def answer_callback(msg_id, answer):
            total_answer = {
                'id': msg_id,
                'ans': answer,
            }
            QueueWrapper.send_message(answer_queue_name, dump_object(total_answer))

        def req_callback(ch, method, properties, body):
            body = load_object(body)
            ack = request_callback(body['req'], lambda answer: answer_callback(body['id'], answer))
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        QueueWrapper.subscribe_to_queue(callback=req_callback,
                                        queue=request_queue_name,
                                        no_ack=False)
    def get_consumer(request_queue_name: str, answer_queue_name: str,
                     answer_callback: Callable[[dict, any], Optional[bool]]):
        def raw_answer_callback(ch: BlockingChannel,
                                method: spec.Basic.Deliver,
                                _: spec.BasicProperties, body: bytes):
            body: AnsBody = load_object(body)
            ack = answer_callback(body['id'], body['ans'])
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        def write_msg(msg_id: dict, request: dict):
            message = {'id': msg_id, 'req': request}
            packed: bytes = dump_object(message)
            QueueWrapper.send_message(request_queue_name, packed)

        QueueWrapper.subscribe_to_queue(callback=raw_answer_callback,
                                        queue=answer_queue_name,
                                        auto_ack=False)
        return write_msg
    def get_consumer(request_queue_name, answer_queue_name, answer_callback):
        def raw_answer_callback(ch, method, properties, body):
            body = load_object(body)
            ack = answer_callback(body['id'], body['ans'])
            if ack is None:
                ack = True
            if ack:
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                ch.basic_nack(delivery_tag=method.delivery_tag)

        def write_msg(msg_id, request):
            message = {
                'id': msg_id,
                'req': request
            }
            packed = dump_object(message)
            QueueWrapper.send_message(request_queue_name, packed)

        QueueWrapper.subscribe_to_queue(callback=raw_answer_callback,
                                        queue=answer_queue_name,
                                        no_ack=False)
        return write_msg