I need to use AWS IoT MQTT service. I am doing some experimenting with https://github.com/aws/aws-iot-device-sdk-python currently.
My application will be using websockets to communicate with another service, and then publish / subscribe to MQTT topics to forward / receive messages.
Is it likely that this library will be blocking code execution? I still try to get my head around asyncio, and not sure what things I should be looking out for. How do I know if it will cause problems?
I believe I will only need to use AWSIoTMQTTClient from above library .
This is an extract from the working code I have:
class AWSIoTClient:
def __init__():
...
self.client = AWSIoTMQTTClient(...)
def subscribe(self, callback):
self.client.subscribe(f'{self.TOPIC}/subscribe/', 0, callback)
def publish(self, message):
self.client.publish(self.TOPIC, message, 0)
class MyWSProtocol(WebSocketClientProtocol):
def set_aws_client(self, client: AWSIoTClient):
client.subscribe(self.customCallback)
self.client = client
def customCallback(self, client, userdata, message):
# This will be called when we send message from AWS
if message.payload:
message = json.loads(message.payload.decode('utf-8').replace("'", '"'))
message['id'] = self.next_id()
self.sendMessage(json.dumps(message).encode('utf-8'))
def onMessage(self, payload, isBinary):
message = json.loads(payload)
# This will forward message to AWS
self.client.publish(str(payload))
You should not allow to having long-running blocking (synchronous) code inside any of your coroutines. It'll lead to blocking your global event loop and further blocking all of your coroutines everywhere.
If you need to run blocking code inside coroutine you should do it in executor (read here about it).
You should keep it in mind when you writing coroutines, but usually asyncio will warn you about this error if you'll enable debug mode:
You'll see warning: