小编amb*_*man的帖子

Python 异步函数返回协程对象

我正在运行一个 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>

python-asyncio python-3.6 azure-iot-sdk azure-iot-edge

2
推荐指数
1
解决办法
7198
查看次数