Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python async function returning coroutine object

I am running a python program to listen to azure iot hub. The function is returning me a coroutine object instead of a json. I saw that if we use async function and call it as a normal function this occurs, but i created a loop to get event and then used run_until_complete function. What am i missing here?

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()

OUTPUT- Message received on input1 <class 'coroutine'> <coroutine object receive_message_on_input at 0x7f1439fe3a98>

like image 917
ambharesh venkatraman Avatar asked Sep 20 '25 10:09

ambharesh venkatraman


1 Answers

Long story short: you just have to write await client.receive_message_on_input("input1"). Your main is a coroutine, but receive_message_on_input is a coroutine as well. You must wait for it to complete. I could tell you the story, but it's too long really. :)

like image 62
Erwin Avatar answered Sep 23 '25 08:09

Erwin