Loading video player…

Introducing a Basic Example

Resource mentioned in this lesson:

00:00 Let’s dive in, shall we? So in your first lesson, I’d like to talk you through a basic example of asynchronous programming. Now, I’m doing this for two reasons.

00:09 Firstly, this example forms the basis for the rest of the course. So I think it makes sense to start there. But secondly, it has the added benefit of being a quick recap of async programming before you learn about iterators and interables.

00:24 However, if async programming is completely new to you, then I include a link to a great Real Python tutorial that will bring you up to speed in no time.

00:35 And this is our example. Say you find yourself at home during the weekend and you need to check your email. So you’ll need to start up your laptop. However, you are also in desperate need of a coffee because it was a bit of a rough night last night, and it just so happens that a parcel has just arrived and it’s waiting in the hall for you to open it.

00:56 Now there are two ways in which you can tackle these three tasks. Firstly, well, you could just start up your laptop and sit there for 10 minutes. And once the laptop has started up, you can then go into the kitchen and make coffee, which will take three minutes.

01:12 And then once the coffee is made, you walk into the hall and you open up your parcel, which will take you another three minutes. So in total, these three tasks will have taken you 16 minutes.

01:25 And that is actually what happens when you code sequentially or synchronously. One task is performed after the other. It means that a task cannot start until the current task is done.

01:38 So the current task is blocking the code.

01:41 Now, a second way of addressing your problem is that you start up your laptop and while your laptop is starting up, you know this will take a little while.

01:49 You walk into the kitchen and you start making your coffee. Now, while the coffee is boiling and brewing, you walk into the hall and you open up your parcel.

01:58 And that takes you about three minutes, by which point your coffee is ready, and all you have to do is wait for your laptop to start up. So in total, this will have taken you 10 minutes as opposed to 16.

02:11 Now to me, this feels like a more natural way of addressing the problem, and that is what you will try to achieve with asynchronous programming. So in this analogy, I’d like you to think of yourself as the CPU.

02:26 So while you were sitting waiting for your laptop to start up, that was effectively the CPU sitting there being idle. So asynchronous programming multitasking involves a more effective use of the CPU’s time.

02:41 Now, as a side note, you could have addressed this problem in a third way. Actually, while you were sitting there waiting for the laptop to start up, you could have asked a second person in the house to make the coffee and the third person to open the parcel for you.

02:57 Now, the result would’ve been the same. After 10 minutes, all three tasks would’ve been done. But this is not async programming. This is not you multitasking. You only did one thing.

03:08 You sat and waited for your laptop to start up. You asking help of others, that is actually multithreading. Now, I just wanted to highlight that because there tends to be some confusion between the two.

03:21 So async programming only uses one CPU. Multithreading uses multiple CPUs.

03:29 In the next lesson then, I will walk you through the code of this async programming example.

Become a Member to join the conversation.