RabbitMQ 测试问题

使用eventlet并发consumer脚本:
eventlet.monkey_patch(all=True)

msg_per_queue = 50
queue_num = 10
rabbit_host = '10.23.54.150:5672'

class Consumer():

    def __init__(self, count):
        self.queue_name = 'test_queue%d' % count
        self.i=0
        self.a=None
        self.b=None
        server = {'host':rabbit_host, 'userid':'guest', 
                  'password':'guest', 'ssl':False}
        self.conn = amqp.Connection( server['host'],
                                userid=server['userid'],
                                password=server['password'],
                                ssl=server['ssl'])
        self.ch = self.conn.channel()
        self.ch.access_request('/data', active=True, read=True)
        self.ch.exchange_declare(exchange='rabbit_test_concurrent',
                                  type='topic', durable=True,
                                  auto_delete=False)
        self.ch.queue_declare(queue=self.queue_name, durable=True, 
                              exclusive=False, auto_delete=False)
        self.ch.queue_bind(queue=self.queue_name, 
                            exchange='rabbit_test_concurrent',
                            routing_key=self.queue_name)

    def showmsg(self, msg):
        if self.i == 0:
            self.a = time.time()
        self.i = self.i + 1
        msg.channel.basic_ack(msg.delivery_tag)
        if self.i == msg_per_queue:
            self.b =time.time()
            msg.channel.basic_cancel(msg.consumer_tag)
        if msg.body == 'quit':
            msg.channel.basic_cancel(msg.consumer_tag)
     
    def __call__(self):
        self.ch.basic_consume(self.queue_name, callback=self.showmsg)
        try:
            while self.ch.callbacks:
                self.ch.wait()
            self.ch.close()
            self.conn.close()
        except KeyboardInterrupt, e:
            print 'hello'
            raise e


if __name__ == '__main__':
    consumers = []
    pool = eventlet.GreenPool()
    try:
        for i in range(1, queue_num+1):
            consumer = Consumer(i)
            consumers.append(consumer)
            pool.spawn_n(consumer)
             
        pool.waitall()
    except KeyboardInterrupt :
        msg_sum = sum(consumer.i for consumer in consumers)
        print 'the sum of received   messages is %d' % msg_sum
        exit()
         
    a = min(consumer.a for consumer in consumers)
    b = max(consumer.b for consumer in consumers)
    if a and b:
        print 'the time used is %f' % (b-a)

运行出现下面问题:

Traceback (most recent call last):
  File "consumer_concurrent_eventlet.py", line 68, in
    consumer = Consumer(i)
  File "consumer_concurrent_eventlet.py", line 27, in __init__
    ssl=server['ssl'])
  File "/usr/lib/python2.6/site-packages/amqplib-1.0.2-py2.6.egg/amqplib/client_0_8/connection.py", line 138, in __init__
    self._x_start_ok(d, login_method, login_response, locale)
  File "/usr/lib/python2.6/site-packages/amqplib-1.0.2-py2.6.egg/amqplib/client_0_8/connection.py", line 719, in _x_start_ok
    self._send_method((10, 11), args)
  File "/usr/lib/python2.6/site-packages/amqplib-1.0.2-py2.6.egg/amqplib/client_0_8/abstract_channel.py", line 76, in _send_method
    method_sig, args, content)
  File "/usr/lib/python2.6/site-packages/amqplib-1.0.2-py2.6.egg/amqplib/client_0_8/method_framing.py", line 252, in write_method
    self.dest.write_frame(1, channel, payload)
  File "/usr/lib/python2.6/site-packages/amqplib-1.0.2-py2.6.egg/amqplib/client_0_8/transport.py", line 165, in write_frame
    frame_type, channel, size, payload, 0xce))
  File "/usr/lib/python2.6/site-packages/eventlet-0.13.0-py2.6.egg/eventlet/greenio.py", line 307, in sendall
    tail = self.send(data, flags)
  File "/usr/lib/python2.6/site-packages/eventlet-0.13.0-py2.6.egg/eventlet/greenio.py", line 293, in send
    total_sent += fd.send(data[total_sent:], flags)
socket.error: [Errno 104] Connection reset by peer

将代码进行如下修改,貌似可解决问题:
if __name__ == '__main__':
    consumers = []
    pool = eventlet.GreenPool()
    try:
        for i in range(1, queue_num+1):
            consumer = Consumer(i)
            consumers.append(consumer)
            pool.spawn_n(consumer)
             
        pool.waitall()
    except KeyboardInterrupt :
        msg_sum = sum(consumer.i for consumer in consumers)
        print 'the sum of received   messages is %d' % msg_sum
        exit()

改为如下:
if __name__ == '__main__':
    consumers = []
    pool = eventlet.GreenPool()
    try:
        for i in range(1, queue_num+1):
            consumer = Consumer(i)
            consumers.append(consumer)

        print '%d connection is open' % i
        for consumer in consumers:
            pool.spawn_n(consumer)

        pool.waitall()
    except KeyboardInterrupt :
        msg_sum = sum(consumer.i for consumer in consumers)
        print 'the sum of received   messages is %d' % msg_sum
        exit()


当发现问题时,第一反映是服务器端可能出现了tcp连接数限制问题,觉得自己脚本正常。通过打印标志得到原来好多链接并没有connection成功。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页