我正在运行一个python程序来监听azure iot集线器。该函数将返回一个协程对象,而不是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()输出-在0x7f1439fe3a98>的input1 <协程><协程对象receive_message_on_input上收到的消息
发布于 2021-07-10 03:02:14
长话短说:你只需要写await client.receive_message_on_input("input1")。您的main是一个协程,但receive_message_on_input也是一个协程。您必须等待它完成。我可以给你讲这个故事,但它实在太长了。:)
https://stackoverflow.com/questions/66768529
复制相似问题