如何将RabbitMQ消息发送给Pykka演员?

时间:2015-02-08 17:52:16

标签: python rabbitmq actor pika pykka

更新2015年8月:对于想要使用消息传递的人,我目前会推荐zeromq。可以作为pykka的补充或替代使用。

我如何收听RabbitMQ队列中的消息,然后将它们转发给Pykka中的演员?

目前,当我尝试这样做时,我会出现奇怪的行为,系统停止运行。

以下是我实施演员的方式:

class EventListener(eventlet.EventletActor):
    def __init__(self, target):
        """
        :param pykka.ActorRef target: Where to send the queue messages.
        """
        super(EventListener, self).__init__()

        self.target = target

    def on_start(self):
        ApplicationService.listen_for_events(self.actor_ref)

这是我在ApplicationService类中的方法,它应该检查队列中的新消息:

@classmethod
def listen_for_events(cls, actor):
    """
    Subscribe to messages and forward them to the given actor.
    """    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='test')
    def callback(ch, method, properties, body):
        message = pickle.loads(body)
        actor.tell(message)

    channel.basic_consume(callback, queue='test', no_ack=True)
    channel.start_consuming()            

似乎start_consuming无限期地阻止了。有没有办法可以自己定期“轮询”队列?

1 个答案:

答案 0 :(得分:3)

您的所有代码对我来说都是正确的。如果要检查每个actor使用的队列,可以检查从actor_inbox返回的actor引用上可用的Actor#start属性。

EventletActor继承时我遇到了类似的问题,所以为了测试我使用EventletActor并使用ThreadingActor尝试了相同的代码。据我所知,他们都使用eventlet来完成工作。 ThreadingActor对我很有用,但EventletActor不适用于ActorRef#tell,它适用于ActorRef#ask

我开始使用同一目录中的两个文件,如下所示。

my_actors.py:初始化两个演员,这些演员将通过打印由其班级名称开头的消息内容来响应消息。

from pykka.eventlet import EventletActor
import pykka


class MyThreadingActor(pykka.ThreadingActor):
    def __init__(self):
        super(MyThreadingActor, self).__init__()

    def on_receive(self, message):
        print(
            "MyThreadingActor Received: {message}".format(
                message=message)
        )


class MyEventletActor(EventletActor):
    def __init__(self):
        super(MyEventletActor, self).__init__()

    def on_receive(self, message):
        print(
            "MyEventletActor Received: {message}".format(
                message=message)
        )


my_threading_actor_ref = MyThreadingActor.start()
my_eventlet_actor_ref = MyEventletActor.start()

my_queue.py:在pika中设置一个队列,向队列发送一条消息,然后转发给两个设置的演员。在告知每个演员关于该消息之后,将检查他们当前的演员收件箱中队列中的任何内容。

from my_actors import my_threading_actor_ref, my_eventlet_actor_ref
import pika


def on_message(channel, method_frame, header_frame, body):
    print "Received Message", body
    my_threading_actor_ref.tell({"msg": body})
    my_eventlet_actor_ref.tell({"msg": body})

    print "ThreadingActor Inbox", my_threading_actor_ref.actor_inbox
    print "EventletActor Inbox", my_eventlet_actor_ref.actor_inbox

    channel.basic_ack(delivery_tag=method_frame.delivery_tag)


queue_name = 'test'
connection = pika.BlockingConnection()

channel = connection.channel()
channel.queue_declare(queue=queue_name)
channel.basic_consume(on_message, queue_name)
channel.basic_publish(exchange='', routing_key=queue_name, body='A Message')

try:
    channel.start_consuming()
except KeyboardInterrupt:
    channel.stop_consuming()

    # It is very important to stop these actors, otherwise you may lockup
    my_threading_actor_ref.stop()
    my_eventlet_actor_ref.stop()
connection.close()

当我运行my_queue.py时,输出如下:

  

收到消息消息

     

ThreadingActor收件箱<Queue.Queue instance at 0x10bf55878>

     

MyThreadingActor收到:{'msg': 'A Message'}

     

EventletActor收件箱<Queue maxsize=None queue=deque([{'msg': 'A Message'}]) tasks=1 _cond=<Event at 0x10bf53b50 result=NOT_USED _exc=None _waiters[0]>>

当我点击CTRL+C以停止队列时,我注意到EventletActor最终收到该消息并将其打印出来:

  收到了

^C MyEventletActor:{'msg': 'A Message'}

所有这些让我相信EventletActor中可能存在错误,我认为您的代码很好并且存在一个我在第一次检查时无法在代码中找到的错误。

我希望这些信息有所帮助。

相关问题