Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWSIoTPythonSDK - UserDefinedException Handling #15

Closed
Venkat2811 opened this issue Sep 14, 2016 · 10 comments
Closed

AWSIoTPythonSDK - UserDefinedException Handling #15

Venkat2811 opened this issue Sep 14, 2016 · 10 comments

Comments

@Venkat2811
Copy link

Hi,

I've been trying publishing messages to my local Mosquitto MQTT broker via Paho client and also to AWS IoT using your SDK. Sometimes, even though internet connectivity is available messages don't get published. After doing some reading, I found that loop_start() and loop_stop() of paho can help to over come this. I tried in my native paho client and also in AWS SDK. Before publishing I'll do loop_start() and after publishing I'll be doing loop_stop(). It's good so far and messages are published.

Is it a good practice ?

Would like to hear your inputs on this.

Thanks,
Venkat

@liuszeng
Copy link
Contributor

Hi @Venkat2811 ,

Thank you very much for using AWS IoT Python SDK!

In the Python Paho client, which is also used in the SDK as the MQTT layer, network traffic is managed in a separate thread. The client itself will not be able to send out MQTT packets unless the network traffic is enabled. Paho has provided severals APIs for managing the network traffic. One of them is loop_start() and loop_stop(). In their documentation:

Calling loop_start() once, before or after connect*(), runs a thread in the background to call loop() automatically. This frees up the main thread for other work that may be blocking. This call also handles reconnecting to the broker. Call loop_stop() to stop the background thread.

If you are using the SDK, it starts the network traffic thread in the background for you upon connect:
https://github.com/aws/aws-iot-device-sdk-python/blob/master/AWSIoTPythonSDK/core/protocol/mqttCore.py#L283

Can you specify more about the issue you are having with the SDK? Are you having trouble publishing messages to AWS IoT? Do you have any error messages when you experiencing the missing publishes issue?

Thanks,
Liusu

@Venkat2811
Copy link
Author

Venkat2811 commented Sep 15, 2016

Hi @liuszeng ,

Thanks for your explanation. Yes after raising the issue, I noticed that you are also using loop_start() in connect() method of SDK.

I tried to recreate the problem, but I couldn't. The problem is that, publish Queue got full and I got queue full exception. Client went offline for about 5 mins. Maybe it is some network issue.

Also, I am unable to catch publishQueueDisabled Exception in my except block ? Why is that so.. ? User defined exceptions are caught only in except Exception block.

I would like to have separate except block for pubishQueueDisabled and publishQueueFull exceptions.

@liuszeng
Copy link
Contributor

Hi @Venkat2811 ,

Sorry for the delayed reply. Thank you very much for providing the information.

From your description, it seems like a disconnect happened in between and SDK client started to queue up outbound publish requests until it reaches the maximum internal queue size. To further investigate into the issue, we will need more debug information for that.

As for exceptions, this is indeed a defect and we will fix it in the future releases. By now, you can use the following workaround:

After you installed "AWSIoTPythonSDK", include the following lines into your code:

import os
import sys
import AWSIoTPythonSDK
sys.path.insert(0, os.path.abspath(AWSIoTPythonSDK.__file__))
# You can start importing the exceptions now
from core.exception.AWSIoTExceptions import publishError
...

Thank you very much for pointing it out and making the SDK better.

Liusu

@Venkat2811
Copy link
Author

Hi @liuszeng ,

I was unable to recreate the issue. Sorry I couldn't provide more debug info.

I included the above lines that you have shared in my program. Still publishQueueFullException, publishQueueDisabledException is not caught in their appropriate except block.

Were you able to do that ?

Thanks,
Venkat

@liuszeng
Copy link
Contributor

liuszeng commented Sep 20, 2016

Hi @Venkat2811 ,

The following is the working code piece (Python 3.5.1, 2.7.3) for this work-around:

import os
import sys
import AWSIoTPythonSDK
sys.path.insert(0, os.path.abspath(AWSIoTPythonSDK.__file__))
from core.exception.AWSIoTExceptions import publishError
from core.exception.AWSIoTExceptions import publishQueueFullException
from core.exception.AWSIoTExceptions import publishQueueDisabledException
...

while True:
    try:
        myAWSIoTMQTTClient.publish("sdk/test/Python", "New Message " + str(loopCount), 1)
    except publishQueueDisabledException as e:
        print("Offline publish queue is disabled!")
    except publishQueueFullException as e:
        print("Offline publish queue is full!")
    loopCount += 1
    time.sleep(1)

Can you try it again and see if the issue still exists?

Thanks,
Liusu

@Venkat2811
Copy link
Author

Hi @liuszeng

I am using Python 2.7.12. I'll check with 2.7.3

Will be expecting fix for this issue in future release. Thank you very much for your help :)

Thanks,
Venkat

@Venkat2811 Venkat2811 changed the title loop_start() and loop_stop() of underlying Paho AWSIoTPythonSDK - UserDefinedException Handling Oct 3, 2016
@Venkat2811
Copy link
Author

Renaming issue

@liuszeng
Copy link
Contributor

Addressed in v1.1.0.

@joseph-mctigue
Copy link

I'm a new guy to the AWS SDK, MQTT and IoT.
I'm trying to test sending a message from Home Assistant to my MQTT (mosquitto) broker to my AWSIoTPythonSDK to the AWS cloud. Am I going about this in the correct path as I described?
If so, how do I reverse this when receiving messages from the AWS cloud?
Thanks for any advice in advance.

@liuszeng
Copy link
Contributor

liuszeng commented Mar 2, 2018

Hi @joseph-mctigue ,

Thank you very much for your interest in AWS IoT Device SDK for Python. Can you open up a new thread to explain your use case and questions in details?

Thanks,
Liusu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants