如何将整数参数发送到 RabbitMQ?

How to send integer parameter to RabbitMQ?

如何使用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()

我尝试将其转换为字符串,然后成功了。 我需要它是整数才能将其插入数据库。

basic_publish wants bytes, not arbitrary values. You'll need to encode your value (and then decode 它是从队列中读取的。

# 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")