azure-iot-sdk-python icon indicating copy to clipboard operation
azure-iot-sdk-python copied to clipboard

RuntimeError: asyncio.run() cannot be called from a running event loop in simple send message example

Open shivapower1985 opened this issue 3 years ago • 2 comments

I was trying to check a device connection in IoT hub where I followed following steps,

  1. created resource group and hub name, created device and set the IOTHUB_DEVICE_CONNECTION_STRING as a environmental variable in my CLI.
  2. I am using python 3.9.7
  3. I run the simple send message example as shown in -- https://github.com/Azure/azure-iot-sdk-python/tree/main/azure-iot-device/samples
  4. I get the following error,

ERROR I GET SHOWN BELOW ----------------- 26 if name == "main": ---> 27 asyncio.run(main()) 28

~\anaconda3\lib\asyncio\runners.py in run(main, debug) 31 """ 32 if events._get_running_loop() is not None: ---> 33 raise RuntimeError( 34 "asyncio.run() cannot be called from a running event loop") 35

RuntimeError: asyncio.run() cannot be called from a running event loop

Help me to solve this problem so that I can check the basic connectivity between the device and Azure IoT Hub

shivapower1985 avatar Jul 26 '22 07:07 shivapower1985

@shivapower1985

This seems to be a common error message when using anaconda, where jupyter is already running an event loop.

Do you need to use async? If not, I'd recommend trying one of the sync samples here, such as send_message.py.

If you do need to use async, just let us know and we can go from there. Modifying the sample to use await will likely rectify the issue.

lev-i avatar Aug 04 '22 20:08 lev-i

@shivapower1985

I was able to set up an anaconda environment to run various samples in the SDK. I also ran them in a Jupyter Notebook. If you're still running into issues, please let me know.

There's a package available for conda that patches asyncio to allow nested event loops here. I was able to run the samples without it, but it might fix the original issue you were running into.

lev-i avatar Aug 12 '22 09:08 lev-i

Closing due to inactivity. If you're still experiencing this issue just let us know and we can re-open this.

lev-i avatar Aug 16 '22 19:08 lev-i