amb*_*man 2 python-asyncio python-3.6 azure-iot-sdk azure-iot-edge
我正在运行一个 python 程序来监听 azure iot hub。该函数返回一个协程对象而不是 json。我看到,如果我们使用异步函数并将其作为普通函数调用,就会发生这种情况,但我创建了一个循环来获取事件,然后使用 run_until_complete 函数。我在这里缺少什么?
async def main():
try:
client = IoTHubModuleClient.create_from_connection_string(connection_string)
print(client)
client.connect()
while True:
message = client.receive_message_on_input("input1") # blocking call
print("Message received on input1")
print(type(message))
print(message)
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
except:
print ( "Unexpected error from IoTHub" )
return
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Run Code Online (Sandbox Code Playgroud)
输出- 在 input1 上收到消息 <class 'coroutine'> <coroutine object receive_message_on_input at 0x7f1439fe3a98>
小智 6
长话短说:你只需要写 await client.receive_message_on_input("input1")。你main是一个协程,但receive_message_on_input也是一个协程。您必须等待它完成。我可以告诉你这个故事,但它真的太长了。:)
| 归档时间: |
|
| 查看次数: |
7198 次 |
| 最近记录: |