Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何向Pykka actor发送RabbitMQ消息?_Python_Rabbitmq_Actor_Pika_Pykka - Fatal编程技术网

Python 如何向Pykka actor发送RabbitMQ消息?

Python 如何向Pykka actor发送RabbitMQ消息?,python,rabbitmq,actor,pika,pykka,Python,Rabbitmq,Actor,Pika,Pykka,2015年8月更新:对于希望使用消息传递的人,我目前建议使用zeromq。可作为pykka的补充或完全替代品使用 如何侦听RabbitMQ队列中的消息,然后将它们转发给Pykka中的参与者 目前,当我尝试这样做时,我会出现奇怪的行为,系统会停止运行 以下是我如何实现我的演员: class EventListener(eventlet.EventletActor): def __init__(self, target): """ :param pykka.A

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\u
正在无限期地阻塞。有没有一种方法可以让我自己定期“轮询”队列?

我觉得您的所有代码都是正确的。如果要检查每个参与者使用的队列,可以在从
actor\start
返回的参与者引用中检查其
actor\u收件箱
属性

我在继承
EventletActor
时遇到了类似的问题,因此为了测试,我使用
EventletActor
ThreadingActor
尝试了相同的代码。从源代码中我可以看出,他们都在使用
eventlet
来完成工作。
ThreadingActor
对我来说非常有用,但是
EventletActor
不适用于
ActorRef\tell
,它适用于
ActorRef\ask

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

my_actors.py
:初始化两个actor,它们将通过打印以其类名开头的消息内容来响应消息

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收件箱

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

EventletActor收件箱

当我点击
CTRL+C
停止队列时,我注意到
EventletActor
最终接收并打印消息:

^C
MyEventletor收到:
{'msg':'A Message'}

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


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

您在一个程序中同时使用
pika
pykka
有什么特别的原因吗?似乎您最好单独使用
pykka
。若要重现此行为,请在此处共享更多代码,如ApplicationServiceClass和其他相关代码?@dano我需要在响应队列消息时运行并发进程。(想一想密集的数据分析)有趣的。。。我停止使用ThreadingActors,因为我需要生成一大堆线程。。。但似乎EventletActors无法与ThreadingActors交互操作,因此必须将每个actor切换到ThreadingActors。事件对我来说仍然是个谜。太糟糕了,pykka没有被更新。