Kombu - Message publishing to rabbitmq error - TypeError: 'str' object is not callable

1.7k Views Asked by At

I want to start off by saying that I'm new to not just python but programming in general. With that in mind this is my issue. I am trying to send some data to my Rabbitmq server. Below is my Python code. The json_data is just a variable that holds some json formatted data.

with Connection("amqp://username:password@test_server.local:5672/#/") as conn:
    channel = conn.channel()
    producer = Producer(channel, exchange = "test_exchange", serializer="json")

    producer.publish(json_data)

print "Message sent"

This produces the following error:

Traceback (most recent call last): File "test.py", line 43, in <module> producer = Producer(channel, exchange = "test_exchange", serializer="json") File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 83, in __init__ self.revive(self._channel) File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 210, in revive self.exchange = self.exchange(channel) TypeError: 'str' object is not callable

Any help would be appreciated. Thanks.

2

There are 2 best solutions below

0
On

After banging my head against my desk I figured out that Python didn't like the calling of the exchange. Then my coworker told me that it might work better if I include the exchange and routing key in the publisher. So now my new code looks like this.

# Creates the exchange and queue and binds them
# If already created on the server side these steps are not needed
exchange = Exchange("test_exchange", "direct", durable=True)
queue = Queue("test_q", exchange = exchange, routing_key = "test")

# The last foreword slash is the virtual host
with Connection("amqp://username:password@hostname:5672/", "/") as conn:
    channel = conn.channel()
    producer = Producer(channel, exchange = exchange, serializer="json")

    for key, value in json_data.items():
        producer.publish(exchange = exchange, routing_key = "test", body = {key:value})

print "Message sent!"
0
On

I am answering the question - as your answer doesn't has the real problem identified i.e. the TypeError: 'str' object is not callable at self.exchange = self.exchange(channel) - states that you're are passing the wrong type to the exchange param i.e. passing a 'str' to exchange:

producer = Producer(channel, exchange = "test_exchange", serializer="json")

Solution: Where ever you be passing value to 'exchange' param it has to be an Exchange object.

Your code has to be:

with Connection("amqp://username:password@test_server.local:5672/#/") as conn:
    channel = conn.channel()
    exchange = Exchange(name='inbound') # the minimal declaration
    producer = Producer(channel, exchange=exchange, serializer="json")

    producer.publish(json_data)

print "Message sent"

For full list of Exchange param - docs.


I encountered the same error at queue declaration i.e.

queue = Queue(name=queue_name, exchange='host_tasks', routing_key=binding_key)
bound_queue = queue(channel) # only once bound, we can call declare(), purge(), delete() on exchange

So declared an Exchange, such thah

exchange = Exchange('host_tasks', 'direct', durable=True)
queue = Queue(name=queue_name, exchange=exchange, routing_key=binding_key)
bound_queue = queue(channel)

Don't forget to import Exchange