Basic Rabbitmq Nack consumer program in python.

125 views
Skip to first unread message

Shafa

unread,
Jun 7, 2019, 5:58:56 AM6/7/19
to rabbitmq-users
Hi all,

I'm looking for a basic Rabbitmq Nack consumer program in python. So far, I have the basic producer program.

cli_ack.py:
import pika, socket

credentials = pika.PlainCredentials('xxxx', '1234')
hostname = socket.gethostname()
parameters = pika.ConnectionParameters(host=socket.gethostbyname(hostname), port=5672, virtual_host='/', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"

channel.queue_declare(queue='hello')
if channel.basic_publish(exchange='', routing_key='hello', body='Hello World!', properties=msg_props):
print ("Message Acknowledged")
else:
print ("Message Lost")

print("[x] Sent 'Hello World!'")
connection.close()


ser_ack.py:
import pika, socket

credentials = pika.PlainCredentials('my_server', '1234')
hostname = socket.gethostname()
parameters = pika.ConnectionParameters(host=socket.gethostbyname(hostname), port=5672, virtual_host='/', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
print(" [x] Received %r" % body)


channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=False)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()


OUTPUT:
>python cli_ack.py
 Message Lost        # But it should be "Message Acknowledged"
 [x] Sent 'Hello World!'

>python ser_ack.py
[*] Waiting for messages. To exit press CTRL+C [x] Received b'Hello World!' # Message is received, but the ack is not sent.

What is the correct consumer program in python? Thank you.

Luke Bakken

unread,
Jun 7, 2019, 4:21:45 PM6/7/19
to rabbitmq-users
Hello,

The return value of channel.basic_publish does not indicate if the publish succeeded or not.

I updated the documentation source 26 days ago -


I just triggered a build so that this page will be updated -


Thank you,
Luke

Luke Bakken

unread,
Jun 7, 2019, 4:23:10 PM6/7/19
to rabbitmq-users
Also -

Please note that the definitive source of information is the source code itself:

Shafa

unread,
Jun 10, 2019, 4:18:44 AM6/10/19
to rabbitmq-users
Thank you. From the link, https://pika.readthedocs.io/en/latest/examples/blocking_delivery_confirmations.html
The output is the same, 
>python server.py
Message published could not be confirmed.

I still didn't get the desired output.

Luke Bakken

unread,
Jun 10, 2019, 12:13:38 PM6/10/19
to rabbitmq-users
Hello,

I updated a different example. I've just updated the doc to which you linked, and started a build on readthedocs

As I noted before, you can use the source code itself to verify how a method works:

https://github.com/pika/pika/blob/master/pika/adapters/blocking_connection.py#L2170-L2200

Please read the in-line documentation above. You will see that the return value of the method does not indicate publish success.

Thanks -
Luke
Reply all
Reply to author
Forward
0 new messages