【问题标题】:Kombu - Message publishing to rabbitmq error - TypeError: 'str' object is not callableKombu - 消息发布到 rabbitmq 错误 - TypeError: 'str' object is not callable
【发布时间】:2013-08-20 16:40:47
【问题描述】:

首先我想说的是,我不仅是 python 新手,而且是一般编程新手。考虑到这一点,这是我的问题。我正在尝试将一些数据发送到我的 Rabbitmq 服务器。下面是我的 Python 代码。 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"

这会产生以下错误:

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

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python rabbitmq amqp kombu


    【解决方案1】:

    在我的头撞到我的桌子后,我发现 Python 不喜欢交换的调用。然后我的同事告诉我,如果我在发布者中包含交换和路由密钥,它可能会更好。所以现在我的新代码看起来像这样。

    # 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!"
    

    【讨论】:

      【解决方案2】:

      我正在回答这个问题 - 因为您的答案没有确定真正的问题,即 TypeError: 'str' object is not callable at self.exchange = self.exchange(channel) - 表明您将错误的类型传递给 exchange 参数,即传递了“str”给exchange

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

      解决方案:无论您在何处将值传递给“exchange”参数,它都必须是 Exchange 对象。

      您的代码必须是:

      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"
      

      有关 Exchange 参数的完整列表 - docs


      我在队列声明时遇到了同样的错误,即

      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
      

      所以宣布一个交易所,这样的

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

      别忘了导入Exchange

      【讨论】:

        猜你喜欢
        • 2020-12-15
        • 2011-04-26
        • 2017-01-23
        • 2013-05-28
        • 2021-02-03
        • 2022-11-20
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多