1

我在Azure Service Bus开发中使用过滤器订阅了一个主题Python 3.x,当我等待发送到该主题的信息(通过过滤器的信息)时,我无法接收它。

我需要创建一个始终在监听的守护进程,当我收到该信息时,我会将其发送到应用程序的内部服务,因此接收器在循环内的线程中运行While True

我用来接收消息的代码如下:

while True:
    msg = bus_service.receive_subscription_message(topic_name, subscription_name, peek_lock=True)
    print('Mensaje Recibido-->',msg.body)
    data = msg.body
    send_MyApp(data.decode("utf-8"))
    msg.delete()

我运行它时得到的是下一个信息:

Message --> None
Exception in thread Thread-1:
Traceback (most recent call last):
File "..\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "..\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "../Python/ServiceBusSuscription/receive_subscription.py", line 19, in receive_subscription
send_MyApp(data.decode("utf-8"))
AttributeError: 'NoneType' object has no attribute 'decode'

如果我从线程中运行接收器,这就是它显示的错误消息(同样,当超时被跳过时,我应该删除哪个超时,因为在等待它的守护进程中它不能跳过)。基本上,这是相同的错误:

Traceback (most recent call last):
  File "../Python/ServiceBusSuscription/receive_subscription.py", line 76, in <module>
    main()
  File "../Python/ServiceBusSuscription/receive_subscription.py", line 72, in main
    demo(bus_service)
  File "../Python/ServiceBusSuscription//receive_subscription.py", line 25, in demo
    print(msg.body.decode("utf-8"))
AttributeError: 'NoneType' object has no attribute 'decode'

我没有收到我正在等待的信息,也跳过了服务总线超时(我没有编程)。

有谁能够帮我?微软的文档并没有太大帮助,真的。

提前致谢

更新

我认为问题出在 Azure 服务总线以及订阅和筛选器上。实际上,我有 23 个过滤器,我认为 Azure 服务总线仅适用于 1 个订阅 :( 但我不确定这一点。

4

1 回答 1

1

我尝试成功重现您的问题,然后我发现如果您的主题中没有消息,它就发生了。

因此,您需要检查msg.body是否是Nonetype(None)在解码 的字节之前的值或类型msg.body,如下所示。

data = msg.body
if data != None: 
# Or if type(data) == type(b''):
    send_MyApp(data.decode("utf-8"))
    msg.delete()
else:
    ...

希望能帮助到你。

于 2017-08-31T09:02:23.607 回答