我正在我的Ubuntu工作站上運行一些測試。這些基準開始填充隊列,其運行非常緩慢:我們可以加快通過RabbitMQ發佈消息
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秒的時間。根據最高命令,工作站有12個內核,這些內核並不繁忙。有超過8Gb的空閒內存。隊列是否耐用無關緊要。
我們如何加快發送消息?
發佈在多線程? – Gabriele