rabbitmq / rabbitmq-tutorials

Tutorials for using RabbitMQ in various ways

Home Page:http://www.rabbitmq.com/getstarted.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

basic_consume() got an unexpected keyword argument 'auto_ack'

clarabez opened this issue · comments

I am following official doc to create send and receiver files with Python, but I am always having following error message from PyCharm:

channel.basic_consume('hello', callback, auto_ack=True)
TypeError: basic_consume() got an unexpected keyword argument 'auto_ack'

Here is my receive.py:

import pika, sys, os


def main():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()

    channel.queue_declare(queue='hello')

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

    channel.basic_consume('hello', callback, auto_ack=True)

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


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)

I've already changed from:

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

to:

channel.basic_consume('hello', callback, auto_ack=True)

Checking forums around, I saw some messages related to this, but it was not clear to me if this is happening due to some update or something like this.

Any clue for that?

auto_ack is the correct parameter: https://github.com/pika/pika/blob/master/pika/channel.py#L262-L300

Please ensure you are using the latest version of Pika, which is version 1.1.0

https://pypi.org/project/pika/

I just identified the issue.

Actually, auto_ack is a valid parameter, but you also may use no_ack, like explained on docstring for basic_consume():

[...]
:param bool auto_ack: if set to True, automatic acknowledgement mode
            will be used (see http://www.rabbitmq.com/confirms.html).
            This corresponds with the 'no_ack' parameter in the basic.consume
            AMQP 0.9.1 method
[...]

So, my solution worked when I updated my code on receive.py to:

channel.basic_consume(callback, queue='hello', no_ack=True)

it is working fine now!

Thanks!

no_ack works because you are using an old, unsupported version of Pika. The parameter name was changed to auto_ack in version 1.0.0.

that is very weird... see my output for pika, it shows 1.1.0 version:

➜  ~ pip3 show pika
Name: pika
Version: 1.1.0
Summary: Pika Python AMQP Client Library
Home-page: https://pika.readthedocs.io
Author: None
Author-email: None
License: BSD
Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Requires: 
Required-by: