whether you're doing web scraping retrieving data from a database or interacting with an api knowing about asynchronous operations is crucial in this video i'm going to show you how async work how you can write code that runs in parallel so that your application becomes much more efficient before we start i have something for you it's a design guide that i wrote that explains the seven steps that i take when i design a new software application it's available for free at URL_TOKEN design guide contains really practical actionable stuff you can apply immediately to the code you're working on and improve the way you design your software so get it for free at URL_TOKEN design guide i've also put this link in the description of the video now let's take a look at today's example the example that i'll be looking at today is an internet of things service there's a simple main file that creates an internet of things service objects and then registers a few devices smart devices like a hue light a speaker and a smart toilet i don't really want to know what that means but anyway so each of these devices gets registered at the service and then you can run a couple of programs like there's a wake up program which switches on the hue light switches on the speaker and then plays a song on the speaker and there's a sleep program that switches off the hue light and the speaker and then also flushes and cleans the toilets for you which is really useful and then we run these programs there's also a package here local package called iot which contains a couple of classes that we're using here in this main file let me actually make this a little bit smaller so you can see it better there's a devices file that contains the various devices right so there's a hue light device and a smart speaker device and a smart toilet device and each of these have a couple of methods so there's connect disconnect and send message send message gets a message type which is in this file and message type is basically an enum to switch on or off or change the color of light or play a song open and close etc etc and then there's a data class message that contains a device id for whom the message is intended the type of message that you like to send and the string containing some data and then finally we have a service which has a helper function to generate an id it uses a protocol to define what it expects when a device is registered in the service so the device should have a connect disconnect and send message method and the service itself is pretty straightforward you can register devices unregister them it basically keeps track of devices in a dictionary and generates ids when you register them and then we have a run program method that takes a list of messages and then sends them to the right device by using this send message method here that looks up the device in the dictionary and then sends the message so pretty straightforward let me run this program and then i can show you what is the output so you see it gets a couple of connection messages and then when it runs the program it shows what happens when the message is handled now of course these are not real smart devices that i'm connecting to it's mainly an example to show how asynchronous operations are going to work the traditional way of dealing with asynchronous and parallel code is by using threads
START_SELFPROMO_TOKEN before we start i have something for you it's a design guide that i wrote that explains the seven steps that i take when i design a new software application it's available for free at URL_TOKEN design guide contains really practical actionable stuff you can apply immediately to the code you're working on and improve the way you design your software so get it for free at URL_TOKEN design guide i've also put this link in the description of the video now let's take a look at today's example END_SELFPROMO_TOKEN