Please note: read the guideline before starting.
You're given a skeleton of the IoT service taken from the internet.
As you have noticed — it's fully synchronous and has blocking operations (for example, sleep).
This task aims to make the program work faster (using asyncio
) without breaking its logic.
For easier asynchronous setup, please follow these steps:
-
Make your code use
asyncio
-connect
/disconnect
/send_message
must be async functions (just theasync
/await
interface). Is the code working faster? If the answer is no - then proceed. -
Try to
gather
registering devices — that should be easy. Devices may be registered simultaneously. And when you run the code — wow, it works faster (at least should). That's because each device doesn't need to wait for the previous to register. -
After that,
gather
running programs (inservice.py
) - here the code also may be easy. And if you run it — wow, it works so fast,asyncio
is powerful! But look at the logic — we've broken it: we may start playing music on Speaker before it was turned on, same with flushing and cleaning Smart Toilet. So, despite it working fast, it works incorrectly. -
And finally, your task is to fix these logical problems. As you already have understood, some of these operations may be performed in "parallel", and some of them must still run in a sequence (flush -> clean). So, you may use these helper functions:
from typing import Any, Awaitable async def run_sequence(*functions: Awaitable[Any]) -> None: for function in functions: await function async def run_parallel(*functions: Awaitable[Any]) -> None: await asyncio.gather(*functions)
You have to change the
wake_up_program
andsleep_program
blocks and their run — to the combination of therun_sequence
andrun_parallel
sending messages to the service to make it more logical. Please do that separately, firstly forwake_up
, then forsleep
.Hint: Remove variables
wake_up_program
andsleep_program
andrun the programs
block. Instead, you should combinerun_sequence
andrun_parallel
with the same commands fromwake_up_program
andsleep_program
. The parallel running is always preferred, because it is faster, but logical order should be taken into account (for ex: switch on -> play; flush -> clean). After that, your code will work a bit longer (still faster than at the beginning), but at least it works correctly now.
Please note: attach the screenshot of your script results (the console) to the PR.