class AddService:
    name = 'add_service'

    @event_handler('productor_service', 'a')
    @event_handler('productor_service', 'b')
    def handle_message(self, payload: str):
        logger.debug(f'handle_message {payload}')

    @rpc
    @context_id_dec
    def add(self, a: int, b: int, /, context_id: str) -> int | None:
        try:

            logger.debug(f'收到一个加法计算请求:{a},{b},{asgi_context_id.get()}')
            # time.sleep(1000)
            return a+b
        except Exception as error:
            logger.exception(error)
            return None

    @http('GET', '/hi')
    def hi(self, request):
        return "hello world"

    @http('GET', '/ha')
    def ha(self, request):
        time.sleep(10)
        return 'ok'

nameko run services:AddService --config ./config.yaml Use this command and find that there is only one connection:

图片.png

 class TrashCanService:
    name = 'trash_scan_service'

    @event_handler('hi_api', 'rubbish')
    def receive(self, message: str):
        logger.debug(f'开始消费消息, {message}')
        response = requests.get('http://127.0.0.1:50010')
        logger.debug(f'{message}, {response.text}')

    receive.create_queue_only = True

    @event_handler('drop_throw_rubbish_service', 'drop_rubbish')
    def receive_by_rpc(self, message: str):
        pass


class AddService:
    name = 'add_service'

    @event_handler('productor_service', 'a')
    @event_handler('productor_service', 'b')
    def handle_message(self, payload: str):
        logger.debug(f'handle_message {payload}')

    @rpc
    @context_id_dec
    def add(self, a: int, b: int, /, context_id: str) -> int | None:
        try:

            logger.debug(f'收到一个加法计算请求:{a},{b},{asgi_context_id.get()}')
            # time.sleep(1000)
            return a+b
        except Exception as error:
            logger.exception(error)
            return None

    @http('GET', '/hi')
    def hi(self, request):
        return "hello world"

    @http('GET', '/ha')
    def ha(self, request):
        time.sleep(10)
        return 'ok'

But this way, two services, a total of two amqp connections:

图片.png

So nameko 是一个服务一个 amqp 连接,还是每个队列 work 函数一个 amqp 连接? the conclusion of this question is: nameko is a service an amqp connection


universe_king
3.4k 声望678 粉丝