0

如何使用 aiormq 向 rabbitMQ 发送整数参数。有了这个:

async def save_to_db(number: int):
    # Perform connection
    connection = await aiormq.connect("amqp://guest:guest@" + rabbitmqHost + "/")

    # Creating a channel
    channel = await connection.channel()

    # Sending the message
    await channel.basic_publish(number, routing_key=queueName)

我正进入(状态:

TypeError: object of type 'int' has no len()

我试图将它转换为字符串,然后它起作用了。我需要它是整数才能将其插入数据库。

4

1 回答 1

1

basic_publish想要字节,而不是任意值。您需要对您的值进行编码(然后在从队列中读取它时对其进行解码)。

# Sending the message
await channel.basic_publish(number.to_bytes(2, byte_order="big"))

# Receiving the message
async def on_Message(message):
    number = int.from_bytes(message.body, byte_order="big")
于 2021-04-30T14:32:35.710 回答