Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic_publish not sending messages #1324

Closed
averri opened this issue Jul 3, 2021 · 3 comments
Closed

basic_publish not sending messages #1324

averri opened this issue Jul 3, 2021 · 3 comments

Comments

@averri
Copy link

averri commented Jul 3, 2021

I'm using pika==1.1.0. Please refer to the following examples in order to reproduce.

  1. Start the RabbitMQ server (using Docker):
docker run -p 5672:5672 -p 15672:15672 -d  bitnami/rabbitmq:3.8.18
  1. Create the following Python modules:

pika_common.py:

from contextlib import contextmanager
from pika import BlockingConnection, PlainCredentials, ConnectionParameters


@contextmanager
def get_channel(queue: str) -> BlockingConnection:
    credentials = PlainCredentials('user', 'bitnami')
    config = ConnectionParameters(host='localhost', credentials=credentials)
    connection = BlockingConnection(config)
    channel = connection.channel()
    channel.queue_declare(queue=queue,
                          durable=True,
                          arguments={'x-max-priority': 10})
    try:
        yield channel
    finally:
        if connection is not None:
            connection.close()
            print('Connection closed.')

pika_receiver.py:

from pika_common import get_channel


def on_message(ch, method, properties, body):
    print(" Received %r" % body)


def receive():
    queue = 'hello'
    with get_channel(queue) as channel:
        channel.basic_consume(queue, auto_ack=True, on_message_callback=on_message)
        try:
            print('Start consuming.')
            channel.start_consuming()
        finally:
            print('Stop consuming.')
            channel.stop_consuming()


if __name__ == '__main__':
    receive()

pika_sender.py:

from pika_common import get_channel
from pika import BasicProperties

MSG_PROPERTIES = BasicProperties(priority=5, delivery_mode=2)


def send():
    queue = 'hello'
    with get_channel(queue) as channel:
        try:
            channel.basic_publish(exchange='',
                                  routing_key=queue,
                                  body='My message body!',
                                  properties=MSG_PROPERTIES,
                                  mandatory=True)

            print("Message published.")

        except Exception as e:
            print(e)


if __name__ == '__main__':
    send()
  1. Start the receiver:
python pika_receiver.py
  1. Start the sender:
python pika_sender.py
  1. Verify that the sender is blocked, and the receiver never receives the message.
@averri
Copy link
Author

averri commented Jul 3, 2021

I have found that if RabbitMQ is running low on any resource, the BlockingConnection will block until resolving the low resorce!

My disk has less available capacity than the low watermark:
image

After allocating more space for RabbitMQ, the above code worked perfectly.

References:
https://pika.readthedocs.io/en/stable/modules/adapters/blocking.html

@goclos
Copy link

goclos commented Apr 13, 2022

Hi,
Thanks! I had exactly same problem on GKE. Now it works perfectly.

@lukebakken
Copy link
Member

Sorry, I was unsubscribed from this repository and have missed new issues from the past few months. Thanks everyone for their input here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants