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

hello_world_producer_pubconfirm.py #9

Open
mgruenberg opened this issue Jan 1, 2013 · 10 comments
Open

hello_world_producer_pubconfirm.py #9

mgruenberg opened this issue Jan 1, 2013 · 10 comments

Comments

@mgruenberg
Copy link

hello_world_producer_pubconfirm.py fails with the following when using pika 0.98

Traceback (most recent call last):
File "/usr/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/usr/lib/python2.7/bdb.py", line 387, in run
exec cmd in globals, locals
File "", line 1, in
File "hello_world_producer_pubconfirm.py", line 12, in
import pika, sys
TypeError: confirm_delivery() got an unexpected keyword argument 'callback'

The issue is its using a BlockingConnection and with a blocking connection it does not allow for a callback:

/usr/local/lib/python2.7/dist-packages/pika-0.9.8-py2.7.egg/pika/adapters/blocking_connection.py(431)confirm_delivery()

def confirm_delivery(self, nowait=False)

@fatih
Copy link

fatih commented Feb 9, 2013

Second here, the example in the book doesn't work due the blocking connection error.

@DrBill37
Copy link

DrBill37 commented Apr 3, 2013

Third here, the example in the book doesn't work due the blocking connection error.

@videlalvaro
Copy link
Collaborator

The examples in the book were tested against pika-0.9.6

@DrBill37
Copy link

DrBill37 commented Apr 3, 2013

Then I guess the examples in github which I retrieved are also out of sync with the latest version of pika which you get with easy_install... perhaps the Python 'hello_world_producer_pubconfirm.py' example could be updated for the latest version of pika?

-thanks

@scottcc
Copy link

scottcc commented May 21, 2013

Certainly the semantics of confirms have changed since 0.9.6. From what I can see, the callback parameter was removed from BlockingChannel (blocking_connection.py) in pika/pika@f2f1d95.

The way to use Confirms with BlockingConnections seems now to be listed as a doc topic here: https://pika.readthedocs.org/en/latest/examples/blocking_delivery_confirmations.html

So it seems the example would have to be rewritten a bit to use the result of basic_publish. It seems that the return value (for channels with confirm_delivery turned on) from basic_publish will be True if it's confirmed, False otherwise.

    def basic_publish(self, exchange, routing_key, body,
                      properties=None, mandatory=False, immediate=False):
# documentation snipped
# leadup sanity code snipped

        if self._confirmation:
            response = self._rpc(spec.Basic.Publish(exchange=exchange,
                                                    routing_key=routing_key,
                                                    mandatory=mandatory,
                                                    immediate=immediate),
                                 None,
                                 [spec.Basic.Ack,
                                  spec.Basic.Nack],
                                 (properties, body))
            if mandatory and self._response:
                response = self._response[0]
                LOGGER.warning('Message was returned (%s): %s',
                               response.reply_code,
                               response.reply_text)
                return False

            if isinstance(response.method, spec.Basic.Ack):
                return True
            elif isinstance(response.method, spec.Basic.Nack):
                return False
            else:
                raise ValueError('Unexpected frame type: %r', response)

So probably not a five minute endeavour :)

@davidgood1
Copy link

With a BlockingConnection, it was about a 5 minute endeavor!

import pika, sys

credentials = pika.PlainCredentials("guest", "guest")
conn_params = pika.ConnectionParameters("localhost", credentials = credentials)
conn_broker = pika.BlockingConnection(conn_params)

channel = conn_broker.channel()

channel.confirm_delivery()

msg = sys.argv[1]
msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"

if channel.basic_publish(body = msg,
                         exchange = "hello-exchange",
                         properties = msg_props,
                         routing_key = "hola"):
    print 'Confirm received!'
else:
    print 'Message lost!'

channel.close()

You'll notice that the code no longer uses a callback or msg_id references at all, which is a more consistent and simple approach when using a blocking channel. This means that most of section 2.7 of the book is not relevant to BlockingConnection channels anymore.

@adasfan
Copy link

adasfan commented Jul 7, 2016

is the call to channel.confirm_delivery() even necessary?

@zulrang
Copy link

zulrang commented Jul 21, 2016

is the call to channel.confirm_delivery() even necessary?

Yes. It puts the channel in confirm mode

@sandrohuber
Copy link

The solution proposed by davidgood1 is not valid anymore. The current approach is documented in https://github.com/pika/pika/blob/a3972b9b8fd8849dfb69a40548d23f18f4368abb/docs/examples/blocking_delivery_confirmations.rst

@huangshaoqi
Copy link

huangshaoqi commented Dec 5, 2020

"""
def basic_publish(self,
                      exchange,
                      routing_key,
                      body,
                      properties=None,
                      mandatory=False):
....
....
...

:raises UnroutableError: raised when a message published in
            publisher-acknowledgments mode (see
            `BlockingChannel.confirm_delivery`) is returned via `Basic.Return`
            followed by `Basic.Ack`.
:raises NackError: raised when a message published in
            publisher-acknowledgements mode is Nack'ed by the broker. See
            `BlockingChannel.confirm_delivery`.
"""

import pika, sys
from pika.exceptions import UnroutableError, NackError

credentials = pika.PlainCredentials('guest', 'guest')
conn_params = pika.ConnectionParameters("localhost",
                                        credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params)

channel = conn_broker.channel()

channel.confirm_delivery()

msg = sys.argv[1]
msg_props = pika.BasicProperties(delivery_mode=2)
msg_props.content_type = "text/plain"

try:
    channel.basic_publish(body=msg,
                          exchange="hello-exchange",
                          properties=msg_props,
                          routing_key="hola")
    print('Message publish was confirmed')
except UnroutableError:
    print('Message could not be confirmed')
except NackError:
    print('Message lost!')

channel.close()

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

10 participants