4

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>

0

1 Answer 1

9

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. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Please do tell the story. Or at least add the link. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.