【发布时间】:2014-08-16 21:38:14
【问题描述】:
我正在使用 Pika 0.98 测试 RabbitMQ 的生产者消费者示例。我的生产者在本地 PC 上运行,消费者在 Amazon 的 EC2 实例上运行。
我的生产者坐在一个循环中,每秒发送一些系统属性。问题是我只看到消费者阅读每条第二条消息,就好像每条第二条消息都没有被阅读。例如,我的生产者打印出这个(时间戳、使用的 cpu pct、使用的 RAM):
2014-08-16 14:36:17.576000 -0700,16.0,8050806784 2014-08-16 14:36:18.578000 -0700,15.5,8064458752 2014-08-16 14:36:19.579000 -0700,15.0,8075313152 2014-08-16 14:36:20.580000 -0700,12.1,8074121216 2014-08-16 14:36:21.581000 -0700,16.0,8077778944 2014-08-16 14:36:22.582000 -0700,14.2,8075038720但我的消费者正在打印这个:
收到'2014-08-16 14:36:17.576000 -0700,16.0,8050806784' 收到'2014-08-16 14:36:19.579000 -0700,15.0,8075313152' 收到'2014-08-16 14:36:21.581000 -0700,16.0,8077778944'生产者的代码是:
import pika
import psutil
import time
import datetime
from dateutil.tz import tzlocal
import logging
logging.getLogger('pika').setLevel(logging.DEBUG)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='54.191.161.213'))
channel = connection.channel()
channel.queue_declare(queue='ems.data')
while True:
now = datetime.datetime.now(tzlocal())
timestamp = now.strftime('%Y-%m-%d %H:%M:%S.%f %z')
msg="%s,%.1f,%d" % (timestamp, psutil.cpu_percent(),psutil.virtual_memory().used)
channel.basic_publish(exchange='',
routing_key='ems.data',
body=msg)
print msg
time.sleep(1)
connection.close()
消费者的代码是:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='0.0.0.0'))
channel = connection.channel()
channel.queue_declare(queue='hello')
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
channel.start_consuming()
【问题讨论】:
-
很好的建议,我会仔细检查。
-
@dano 我认为你的回答是正确的,我重新启动了我的服务器,它现在可以工作了。请。如果您想被标记为“正确”,请作为单独的答案输入。 :)
-
酷,很高兴你能成功。我也将我的评论转换为答案。