mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.23k stars 187 forks source link

Deplete All CPU When get message from "queue.get" #477

Closed strongbugman closed 2 years ago

strongbugman commented 2 years ago

I write some code like :

            while self.running:
                try:
                    message = await queue.get(timeout=self.timeout)
                    if message:
                        await self.on_message_receive(message.body, message)
                except aio_pika.exceptions.QueueEmpty:
                    pass

It worked well but depleted all CPU resourse:

image

Does it meet your expectations? Or there is some thing I missed?

PS: I use this solution for now:

....
                except aio_pika.exceptions.QueueEmpty:
                    await asyncio.sleep(0.1)
....
mosquito commented 2 years ago

Your code make useless workload to the rabbitmq. Your code send frame each .get() and receive an answer each iteration, instead of send consume frame and awaiting incoming messages. This make unexpected workload to the server. You should rewrite it using consume or async for queue.iterator().

Also I recommend configure QoS for optimize memory consumption.

strongbugman commented 2 years ago

I got it, the queue.get confused me, thx.