[英]Kombu - Message publishing to rabbitmq error - TypeError: 'str' object is not callable
I want to start off by saying that I'm new to not just python but programming in general. 首先,我想说我不仅是python的新手,而且还是编程的新手。 With that in mind this is my issue. 考虑到这一点,这是我的问题。 I am trying to send some data to my Rabbitmq server. 我正在尝试将一些数据发送到Rabbitmq服务器。 Below is my Python code. 下面是我的Python代码。 The json_data is just a variable that holds some json formatted data. json_data只是一个变量,其中包含一些json格式的数据。
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
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.revive(self._channel)
File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 83, in __init__
File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 210, in revive
self.exchange = self.exchange(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. 谢谢。
After banging my head against my desk I figured out that Python didn't like the calling of the exchange. 将头撞到桌子上后,我发现Python不喜欢调用交易所。 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!"
I am answering the question - as your answer doesn't has the real problem identified ie 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 ie passing a ' str
' to exchange
: 我正在回答这个问题-因为您的答案没有发现真正的问题,即TypeError: 'str' object is not callable at self.exchange = self.exchange(channel)
-表示您传递的是错误的类型到exchange
参数,即传递' str
'来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. 解决方案 :无论您要将价值传递给“ 交换 ”参数,它都必须是Exchange
对象。
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 . 有关Exchange参数文档的完整列表。
I encountered the same error at queue declaration ie 我在队列声明时遇到了相同的错误,即
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
别忘了导入Exchange
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.