我写了这个 python 脚本,它通过修改 RabbitMQ 教程中的默认 RPC 示例充当 RPC 服务器,找到 here .它在我的笔记本电脑上运行良好。但是当我在具有这些规范的亚马逊 ec2 高 CPU 中型实例中运行它时:
1.7 GiB of memory
5 EC2 Compute Units (2 virtual cores with 2.5 EC2 Compute Units each)
350 GB of instance storage
它占用了 100% 的 CPU。尽管我的笔记本电脑配置几乎相同,但它的 CPU 使用率不到 4%。我在我的笔记本电脑和亚马逊上都在 Ubuntu-12.04 中运行它。
这是我的代码
#!/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()
我该如何解决这个问题?
最佳答案
终于找到问题了。这是 Pika 的一个错误,我从 rabbitmq 的邮件列表中得到了这个信息。我通过pypi安装了鼠兔。 pip install pika
.
为了解决这个问题,我卸载了鼠兔
pip 卸载 pika
并从 git 重新安装它
pip install git+https://github.com/pika/pika.git
。
然后就解决了。
关于python - RabbitMQ python worker 脚本使用 100% CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870832/