Skip to content

How to handle publishTimeoutException() #84

@flycast

Description

@flycast

I have a python script that is simulating data being published in the kinds of timing and pauses that I expect to be getting in real life. It publishes messages randomly between 1 - 6 seconds. After each loop it randomly determines to take a nap of up to 600 seconds.

I am getting an expection:

publishTimeoutException()

That exception always seems to come while it is publishing messages on the 1-6 second interval. It is publishing most messages (>99%).

What causes this message? I assume a lack of connection?
What is the best practice for handling this exception...reconnect and republish?

Here is my code with a try, exception added:

myMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myMQTTClient.configureConnectDisconnectTimeout(60)  # 10 sec
myMQTTClient.configureMQTTOperationTimeout(20)  # 5 sec

#Variables
cycleBase = 1
setUpGap = 25

myMQTTClient.connect()
while True:
    if random.uniform(0,100) < 100-(100/setUpGap):
        #Do a cycle
        message = "{ \"timeStamp\":" + str(time.time()) + ", \"thing\":\"testing/IoT\" }"
        try:
            myMQTTClient.publish("testing/IoT", message, 1)
        except:
            print(err)
            myMQTTClient.connect()
            myMQTTClient.publish("testing/IoT", message, 1)

        
        
        time.sleep(cycleBase + random.uniform(0,5))
        print(message)
    else:
        #Do a set up
        setUpGap = random.uniform(120, 600)
        print("Set up for " + str(setUpGap))
        time.sleep(setUpGap)
    
myMQTTClient.disconnect()

Metadata

Metadata

Assignees

No one assigned

    Labels

    guidanceQuestion that needs advice or information.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions