nameko / nameko

Python framework for building microservices
https://www.nameko.io
Apache License 2.0
4.71k stars 470 forks source link

Pls. provide the example of how to handle the event outside nameko #571

Closed wyattsuen closed 6 years ago

wyattsuen commented 6 years ago

Thanks very much for this very beatiful framework. I just use nameko in a week, and switch my most work architechture to it. I have to use kombu to accept the event message in tornado. As I see the kombu document for these days, I find it difficult for me to understand the process without the rabbitmq experience. Can you help me to provide the example how to get event_handler outside nameko using kombu.

class ServiceA:
    """ Event dispatching service. """
    name = "service_a"

    dispatch = EventDispatcher()

    @rpc
    def dispatching_method(self, payload):
        self.dispatch("event_type", payload)

I make a simple example for it. One process can accept the message. But two processes will accept only a part of messages each. I can't find how to get the broadcast message in many processes. Because tornado websocket can not use sub thread to accept the 'while true' message, how to write the 'while true' in 'async def' coroutine to 'await drain_events()'?

from kombu import Connection, Exchange, Queue
exchange=Exchange('service_a.events', 'topic', durable=True, auto_delete=True)
queue=Queue('hello', exchange=exchange, routing_key='event_type')

def process_msg(body, message):
    print(body)
    message.ack()

with Connection('pyamqp://guest:guest@localhost') as conn:
    with conn.Consumer(queue, callbacks=[process_msg]) as consumer:
        while True:
            conn.drain_events()

How to accept this event with SERVICE_POOL and BROADCAST method in kombu without nameko. I think the example will be very helpful to add to the nameko document. Or it is best to provide a standalone event_handler just like the standalone rpc service.

Although I have not met the problem, but I think it useful to send the event outside nameko, and accept the event in nameko is also welcome. Guessing tornado accept a message from user, and I want to send this message to many potential services, maybe broadcast the event is useful.

class ServiceB:
    """ Event listening service. """
    name = "service_b"

    @event_handler("service_a", "event_type")
    def handle_event(self, payload):
        print("service b received:", payload)

How to send the event with SERVICE_POOL and BROADCAST method in kombu without nameko. The examples are very important for us without rabbitmq experiences. So kindly and patiently providing the examples is welcome. Thanks a lot.

mattbennett commented 6 years ago

Your message to the mailing list got stuck in moderation, so I've answered it there rather than on this duplicate.