在 Azure 服务总线上接收有关主题订阅的信息
Receive information about a topic subscription on Azure Service Bus
我在 Azure Service Bus
中使用过滤器订阅了一个主题,使用 Python 3.x
开发,当我等待发送到该主题的信息(通过过滤器的信息)时,我无法接收它。
我需要创建一个始终监听的守护进程,当我收到该信息时,我将其发送到我的应用程序的内部服务,因此接收方 运行 在一个循环中的线程中 运行 =15=]
我用来接收消息的代码如下:
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'
我没有收到我正在等待的信息,并且还跳过了服务总线超时(我没有编程)。
有人可以帮助我吗? Microsoft 的文档并没有多大帮助,真的。
提前致谢
更新
我认为问题出在 Azure 服务总线以及订阅和筛选器上。实际上,我有 23 个筛选器,我认为 Azure 服务总线仅适用于 1 个订阅:( 但我不确定这一点。
我试过成功重现你的问题,然后我发现如果你的主题中没有消息,它就会发生。
因此在解码msg.body
的字节之前,您需要检查msg.body
的值或类型是None
还是type(None)
,如下所示。
data = msg.body
if data != None:
# Or if type(data) == type(b''):
send_MyApp(data.decode("utf-8"))
msg.delete()
else:
...
希望对您有所帮助。
我在 Azure Service Bus
中使用过滤器订阅了一个主题,使用 Python 3.x
开发,当我等待发送到该主题的信息(通过过滤器的信息)时,我无法接收它。
我需要创建一个始终监听的守护进程,当我收到该信息时,我将其发送到我的应用程序的内部服务,因此接收方 运行 在一个循环中的线程中 运行 =15=]
我用来接收消息的代码如下:
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'
我没有收到我正在等待的信息,并且还跳过了服务总线超时(我没有编程)。
有人可以帮助我吗? Microsoft 的文档并没有多大帮助,真的。
提前致谢
更新
我认为问题出在 Azure 服务总线以及订阅和筛选器上。实际上,我有 23 个筛选器,我认为 Azure 服务总线仅适用于 1 个订阅:( 但我不确定这一点。
我试过成功重现你的问题,然后我发现如果你的主题中没有消息,它就会发生。
因此在解码msg.body
的字节之前,您需要检查msg.body
的值或类型是None
还是type(None)
,如下所示。
data = msg.body
if data != None:
# Or if type(data) == type(b''):
send_MyApp(data.decode("utf-8"))
msg.delete()
else:
...
希望对您有所帮助。