You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If, instead of re-instantiating a Session object upon each connection, you attempt to re-use a previous one, receive patterns will raise a StopAsyncIteration exception through the context managed incoming data generator.
This can happen from:
Network failure
SAS Token expiration
It does not occur when manually disconnecting.
Sample code demonstrating behavior (wait for SAS Token to expire, or disconnect from internet to force a drop):
importasyncioimportosfromazure.iot.deviceimportIoTHubSession, MQTTConnectionDroppedError, MQTTConnectionFailedErrorimportlogginglogger=logging.basicConfig(level=logging.DEBUG)
CONNECTION_STRING=os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
TOTAL_MESSAGES_RECEIVED=0asyncdefmain():
globalTOTAL_MESSAGES_RECEIVEDprint("Starting C2D sample")
print("Press Ctrl-C to exit")
session=IoTHubSession.from_connection_string(CONNECTION_STRING, sastoken_ttl=200)
whileTrue:
try:
print("Connecting to IoT Hub...")
asyncwithsessionassession:
print("Connected to IoT Hub")
asyncwithsession.messages() asmessages:
print("Waiting to receive messages...")
asyncformessageinmessages:
TOTAL_MESSAGES_RECEIVED+=1print("Message received with payload: {}".format(message.payload))
exceptMQTTConnectionDroppedError:
# Connection has been lost. Reconnect on next pass of loop.print("Dropped connection. Reconnecting in 1 second")
awaitasyncio.sleep(1)
exceptMQTTConnectionFailedError:
# Connection failed to be established. Retry on next pass of loop.print("Could not connect. Retrying in 10 seconds")
awaitasyncio.sleep(10)
if__name__=="__main__":
try:
asyncio.run(main())
exceptKeyboardInterrupt:
# Exit application because user indicated they wish to exit.# This will have cancelled `main()` implicitly.print("User initiated exit. Exiting")
finally:
print("Received {} messages in total".format(TOTAL_MESSAGES_RECEIVED))
The text was updated successfully, but these errors were encountered:
If, instead of re-instantiating a Session object upon each connection, you attempt to re-use a previous one, receive patterns will raise a
StopAsyncIteration
exception through the context managed incoming data generator.This can happen from:
It does not occur when manually disconnecting.
Sample code demonstrating behavior (wait for SAS Token to expire, or disconnect from internet to force a drop):
The text was updated successfully, but these errors were encountered: