I am very new to RabbitMq and really need to understand how to do it with Python. Because from the tutorial I only wrote the code below which only sends 'hello world!' as a string:
import pika
import random
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
)
channel = connection.channel()
channel.queue_declare(queue="hello")
channel.basic_publish(exchange='', routing_key='hello', body='hello world!')
print(" [x] Sent 'hello world!'")
connection.close()
But I need to produce messages to the broker with random but continuous values from 0 to 100. Can someone help me with what should I add to the code above to do this???? Or any tutorial advice for me? Thx from now..
Try this:
import pika
from random import randint
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
)
channel = connection.channel()
channel.queue_declare(queue="hello")
previous=0
while True:
newnumber=randint(previous+1, 100)
if newnumber>previous:
channel.basic_publish(exchange='', routing_key='hello', body=str(newnumber))
print(f" [x] Sent {newnumber}")
previous=newnumber
if newnumber==100:
break
connection.close()
I think random but continues means this:
[5,34,78,99,100]
creating every number randomly but in continuous order. Another example is [45,60,77,78,83,89,96,98,100]