我对RabbitMq非常陌生,我真的需要了解如何使用Python来实现它。因为在本教程中,我只编写了下面的代码,它只发送'hello world!‘作为字符串:
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()但是,我需要向代理生成从0到100的随机但连续值的消息。有人能帮助我在上面的代码中添加什么来完成这个任务吗?或者给我任何辅导建议?从现在开始..。
发布于 2021-07-25 20:20:58
试试这个:
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()我认为是随机的,但继续的意思是:[5,34,78,99,100]随机地创建每个数字,但顺序是连续的。另一个例子是[45,60,77,78,83,89,96,98,100]
https://stackoverflow.com/questions/68521399
复制相似问题