Python 3.x 我们可以通过RabbitMQ加快消息发布速度吗

Python 3.x 我们可以通过RabbitMQ加快消息发布速度吗,python-3.x,rabbitmq,ubuntu-14.04,Python 3.x,Rabbitmq,Ubuntu 14.04,我正在我的Ubuntu工作站上运行一些测试。这些基准测试从填充一个运行非常缓慢的队列开始: import pika import datetime if __name__ == '__main__': try: connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.cha

我正在我的Ubuntu工作站上运行一些测试。这些基准测试从填充一个运行非常缓慢的队列开始:

import pika
import datetime

if __name__ == '__main__':
    try:
        connection = pika.BlockingConnection(pika.ConnectionParameters(
                host='localhost'))
        channel = connection.channel()

        channel.queue_declare(queue='hello_durable', durable=True)
        started_at = datetime.datetime.now()
        properties = pika.BasicProperties(delivery_mode=2)
        for i in range(0, 100000):
            channel.basic_publish(exchange='',
                                  routing_key='hello',
                                  body='Hello World!',
                                  properties=properties)
            if i%10000 == 0:
                duration = datetime.datetime.now() - started_at
                print(i, duration.total_seconds())
        print(" [x] Sent 'Hello World!'")
        connection.close()
        now = datetime.datetime.now()
        duration = now - started_at
        print(duration.total_seconds())
    except Exception as e:
        print(e)
发送10K消息需要30秒以上。根据top命令,工作站有12个内核,它们并不繁忙。有超过8Gb的可用内存。队列是否持久并不重要


我们怎样才能加快发送消息的速度

Am假设您不运行任何消费者
这些基准测试从填充队列开始。
由于您只发布消息,rabbitmq将切换到流状态。更准确地说,您的交换和/或队列进入流状态。
引自

这(大致)意味着客户受到利率限制;会的 希望发布速度更快,但服务器无法跟上


我敢肯定,如果你仔细观察,你会发现第一部分消息(在初始设置时,使用空队列)运行得很快,但发送速率在某个时候会急剧下降。

从BlockingConnection切换到SelectConnection产生了巨大的差异,将过程加快了近50倍。我所需要做的就是从中修改示例,在循环中发布消息:

import pika

# Step #3
def on_open(connection):

    connection.channel(on_channel_open)

# Step #4
def on_channel_open(channel):

    channel.basic_publish('test_exchange',
                            'test_routing_key',
                            'message body value',
                            pika.BasicProperties(content_type='text/plain',
                                                 delivery_mode=1))

    connection.close()

# Step #1: Connect to RabbitMQ
parameters = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')

connection = pika.SelectConnection(parameters=parameters,
                                   on_open_callback=on_open)

try:

    # Step #2 - Block on the IOLoop
    connection.ioloop.start()

# Catch a Keyboard Interrupt to make sure that the connection is closed cleanly
except KeyboardInterrupt:

    # Gracefully close the connection
    connection.close()

    # Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed
    connection.ioloop.start()

在多线程中发布?我有同样的问题,我不明白在你的上一个示例中,如何通过示例发送日志文件的内容?