I am using rabbitMQ to receive the message. I have 1,000,000 messages need to send out in 1 minute to queue. I am using multiprocessing by python.My code can send over 5 minute. Is that possible to send them in 1 minute in the single pc. Here is my code
import multiprocessing
from datetime import datetime
import pika
import time
import uuid
import sys
class PyPikaTest:
def publish(self,no_message,producer):
c = pika.BlockingConnection(pika.ConnectionParameters(port=5672,virtual_host="test"))
channel = c.channel()
qname = str(uuid.uuid4())
channel.queue_declare(queue='letterbox')
print("start: %s" % (time.ctime(time.time())))
for i in range(1, int(no_message)):
sendtime = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-2]
body = ('aa cccc ' + str(sendtime))
_properties = pika.BasicProperties(
content_type='application/json',
content_encoding='utf-8',
message_id=producer + "_message_no_" + str(i),
timestamp=int(time.time())
)
channel.basic_publish(
exchange='',
routing_key='letterbox',
properties=_properties,
body=body
)
print("end: %s" % (time.ctime(time.time())))
c.close()
def thread_publish(self, no_publisher, no_message):
jobs = []
for i in range(int(no_publisher)):
process = multiprocessing.Process(target=self.publish,args=(no_message, "test_publisher_no_" + str(i)))
jobs.append(process)
#Start the threads (i.e. calculate the random number lists)
for j in jobs:
j.start()
#Ensure all of the threads have finished
for j in jobs:
j.join()
print("List processing complete.")
if __name__ == "__main__":
print('starting .. %s')
x = PyPikaTest()
x.thread_publish(sys.argv[1],sys.argv[2])
Have you tried one of the pika non-blocking connections? I use the AsyncioConnection. When publishing only from a single Python process and I can get 14k-17k messages sent per second, which is about 1mm messages in a minute. This in on a 2018 MacBook Pro i7, RabbitMQ installed locally with Homebrew and Python 3.11.
I created a script where i could attain sending 1mm messages in 61 seconds by chunking it up into 10 batches of 100k asynchronous requests.
For full disclosure I had 4 consumers running, I find that RabbitMQ works best when sending if the queue does not get too large.
The full example code can be found here
This is my program output.