When using Pika, the channel.confirm_delivery() flag must be set before publishing messages. It is important that Pika confirms that each message was sent successfully before sending the next message. However, this will increase the time required to send RabbitMQ messages, since delivery must be confirmed before the program can continue the next message.
channel.confirm_delivery() try: for index in xrange(10): channel.basic_publish(exchange='', routing_key='hello', body='Hello World #%s!' % index) print('Total Messages Sent: %s' % x) except pika.exceptions.ConnectionClosed as exc: print('Error. Connection closed, and the message was never delivered.')
basic_publish will return a Boolean value depending on whether the message was sent or not. But it is important to catch potential exceptions if the connection is closed during transmission and handled accordingly. As in those cases, the exception interrupts the program flow.
source share