使用kombu发布消息 - TypeError'st'对象不可调用

时间:2012-03-09 22:03:04

标签: django kombu

在将我的消息有效负载编码为json之后,我正试图用:

发布到代理
connection = establish_connection()
producer = Producer(channel=connection,
                      exchange="inbound",
                      routing_key="apisubmit")

producer.publish(body=pl,headers={"api_access_key": "xxxx", "client_id": 4, "object_type": "location", "action": "c"})

django正在返回以下内容:

Traceback:
File "/usr/local/pythonenv/openblock/lib/python2.6/site-packages/Django-1.3.1-py2.6.egg/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/pythonenv/openblock/src/myblock/myblock/barz/views.py" in testtwo
  19.     msg=publish_kombu()
File "/usr/local/pythonenv/openblock/src/myblock/myblock/barz/messaging.py" in publish_kombu
  99.                           routing_key="apisubmit")
File "/usr/local/lib/python2.6/dist-packages/kombu/messaging.py" in __init__
  82.         self.exchange = self.exchange(self.channel)

Exception Type: TypeError at /barz/publish_kombu/
Exception Value: 'str' object is not callable

2 个答案:

答案 0 :(得分:2)

Producer.exchange必须是Exchange。默认情况下它是Exchange(""),不知何故你必须将它设置为一个字符串。

答案 1 :(得分:0)

您将价值传递给' 交换' param必须是Exchange对象。

您的代码必须是:

exchange = Exchange(name='inbound') # the minimal declaration
connection = establish_connection()
producer = Producer(channel=connection,
                      exchange=exchange,
                      routing_key="apisubmit")

producer.publish(body=pl,headers={"api_access_key": "xxxx", "client_id": 4, "object_type": "location", "action": "c"})

有关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)

别忘了import Exchange