We recently released a new version of the Python SDK. We’re working hard to improve the experience of our developers and we heard your feedback—the Python SDK is no longer a wrapper around C code, it’s native Python! For those of you who haven’t seen this yet, here’s a quick overview:
What is the v2 Python SDK?
It’s a newly designed, Python-native SDK for working with Azure IoT. If you’re already familiar with our Python SDK, then you know that our first version (v1) was a wrapper around our C SDK. Like anything, this approach had its pros and cons. On the upside, developers familiar with the C SDK could further expand to Python with no underlying functionality differences. However, since the SDK was not in native Python, the developmental ease typically associated with Python was certainly lacking. With this new design, the v2 SDK aims to provide a simplified, more natural experience for Python developers.
Why should you upgrade to v2?
Simply put, because the developer experience is so much better!
Whereas the installation process for v1 involved a series of steps and was highly dependent on your development configuration, setting up v2 is as easy as running a pip command. Additionally, the APIs look and feel what they are—Python native.
To briefly illustrate the new look and feel of the SDK, here's a couple examples of the implementation differences between v1 and v2:
First, for sending telemetry to your IoT Hub, you'd write the following in v1:
message = IoTHubMessage("telemetry message") prop_map = message.properties() prop_map.add("property", "property_value") client.send_event_async(message, send_confirmation_callback, user_ctx)
Whereas in v2, you can simplify with:
message = Message("telemetry message") message.custom_properties["property"] = "property_value" client.send_message(message)
As an even more pronounced example, in v1 you’d write the following for receiving a message from your IoT Hub:
def receive_message_callback(message, counter): global RECEIVE_CALLBACKS message = message.get_bytearray() size = len(message_buffer) print ( "the data in the message received was : <<<%s>>> & Size=%d" % (message_buffer[:size].decode('utf-8'), size) ) map_properties = message.properties() key_value_pair = map_properties.get_internals() print ( "custom properties are: %s" % key_value_pair ) return IoTHubMessageDispositionResult.ACCEPTED client.set_message_callback(message_listener_callback, RECEIVE_CONTEXT)
But in the newly designed v2 SDK, you write:
def message_listener(client): while True: message = client.receive_message() # blocking call print("the data in the message received was ") print(message.data) print("custom properties are") print(message.custom_properties) # Run a listener thread in the background listen_thread = threading.Thread(target=message_listener, args=(device_client,)) listen_thread.daemon = True listen_thread.start()
Extra perks with V2!
While v2 provides all the same support you’ve come to expect for getting your IoT devices up and running, it also has a few changes:
The Python v2 SDK:
We’ve also recently released a new version (2.0.1) that includes support for cloud-to-device messaging and improves MQTT connection resiliency.
We’re excited to see what you build with our new and improved Python SDK!
As we continue to work on improving the experience for Python developers, we encourage you to provide feedback through our open source Github repository and stay tuned on Azure updates for any new IoT SDK announcements.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.