将c2d com从azure IotHub转换为python脚本时,已弃用的方法

2024-10-02 08:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

message = client.receive_message()

这段代码现在已被弃用,在搜索解决方案时,似乎只有我一个人有这个问题。 我得到这个警告:

DeprecatedWarning: receive_message is deprecated as of 2.3.0. We recommend that you use the .on_message_received property to set a handler instead of message = client.receive_message()

如果你有一个可能的解决方案,请张贴在这里。 我正在运行最新的python 3.9和最新的Azure IoT设备库


Tags: of代码clientyou警告messagethatis
1条回答
网友
1楼 · 发布于 2024-10-02 08:15:52

您正在尝试使用不再受支持的方法(因为它已被弃用)。正如错误消息所说,处理C2D消息的正确方法是使用事件处理程序。有一个很好的例子here

您将感兴趣的部分是:

  # define behavior for receiving a message
  # NOTE: this could be a function or a coroutine
  def message_received_handler(message):
      print("the data in the message received was ")
      print(message.data)
      print("custom properties are")
      print(message.custom_properties)
      print("content Type: {0}".format(message.content_type))
      print("")

  # set the mesage received handler on the client
  device_client.on_message_received = message_received_handler

相关问题 更多 >

    热门问题