Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It runs fine in my laptop. But when i run it in an amazon ec2 High CPU Medium Instance with these specs :

1.7 GiB of memory

5 EC2 Compute Units (2 virtual cores with 2.5 EC2 Compute Units each)

350 GB of instance storage

It takes up 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.

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 ?

like image 917
Manu Avatar asked Feb 14 '13 08:02

Manu


1 Answers

Finally found the problem. It was a bug in Pika, i got this information from rabbitmq's mailing list. I had installed pika through pypi. pip install pika.

To fix this i uninstalled pika

pip uninstall pika

and reinstalled it from git

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

And that solved it.

like image 171
Manu Avatar answered Sep 30 '22 12:09

Manu