简体   繁体   English

RabbitMQ python worker脚本使用100%CPU

[英]RabbitMQ python worker script using 100% CPU

I worte this python script which acts as an RPC server by modifying the default RPC example in RabbitMQ tutorial found here . 我通过修改这里找到的RabbitMQ教程中的默认RPC示例来编写这个充当RPC服务器的python脚本。 It runs fine in my laptop. 它在我的笔记本电脑上运行正常 But when i run it in an amazon ec2 High CPU Medium Instance with these specs : 但是当我在亚马逊ec2高CPU中等实例中使用这些规范运行它时:

1.7 GiB of memory 1.7 GiB的内存

5 EC2 Compute Units (2 virtual cores with 2.5 EC2 Compute Units each) 5个EC2计算单元(2个虚拟核,每个具有2.5个EC2计算单元)

350 GB of instance storage 350 GB的实例存储

It takes up 100% CPU. 它占用了100%的CPU。 Although my laptop with almost the same config runs this with less than 4% CPU use.I run this in Ubuntu-12.04 in both my laptop and amazon. 虽然我的笔记本电脑具有几乎相同的配置运行,CPU使用率不到4%。我在笔记本电脑和亚马逊的Ubuntu-12.04中运行它。

Here is my code 这是我的代码

    #!/usr/bin/env python
    import pika
    import commands
    import socket
    import base64

    connection = pika.BlockingConnection(pika.ConnectionParameters(
             host='localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='rpc_queue')
    def on_request(ch, method, props, body):
        #print body
        body = base64.b64decode(body)
        print body
        run = commands.getoutput(body)
        response = socket.gethostname()
        print response
        ch.basic_publish(exchange='',
                        routing_key=props.reply_to,
                        properties=pika.BasicProperties(correlation_id = \
                                                      props.correlation_id),
                        body=str(response))
        ch.basic_ack(delivery_tag = method.delivery_tag)
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(on_request, queue='rpc_queue')
    print " [x] Awaiting RPC requests"
    channel.start_consuming()

How can i fix this ? 我怎样才能解决这个问题 ?

Finally found the problem. 终于找到了问题。 It was a bug in Pika, i got this information from rabbitmq's mailing list. 这是Pika的一个错误,我从rabbitmq的邮件列表中获取了这些信息。 I had installed pika through pypi. 我通过pypi安装了pika。 pip install pika . pip install pika

To fix this i uninstalled pika 解决这个我卸载的鼠兔

pip uninstall pika

and reinstalled it from git 并从git重新安装它

pip install git+https://github.com/pika/pika.git . pip install git+https://github.com/pika/pika.git

And that solved it. 这解决了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM