-
Notifications
You must be signed in to change notification settings - Fork 171
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
Comments
Second here, the example in the book doesn't work due the blocking connection error. |
Third here, the example in the book doesn't work due the blocking connection error. |
The examples in the book were tested against pika-0.9.6 |
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 |
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 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 :) |
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. |
is the call to |
Yes. It puts the channel in confirm mode |
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 |
"""
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() |
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)
The text was updated successfully, but these errors were encountered: