Async Techniques and Examples in Python Transcripts
Chapter: Why async?
Lecture: Demo: Why you need async for speed
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at a real world example of a Python programming that is doing a very poor job taking advantage of the CPU it's running on.
0:09
I'm running on my MacBook that I discussed in the opening, has 12 cores. This is the Intel i9 12 core maxed out you know, all nobs to 11 type of CPU
0:21
for my MacBook Pro, here, so you can see it has a lot of processors. It has a lot of cores. In particular, has 12 hyper-threaded cores
0:30
so six real cores, each one of which is hyper-threaded. Here we have a program running in the background working as hard as it can.
0:38
How much CPU is the entire computer using? Well, 9.5%. If this were 10 years ago, on single core systems the CPU usage would be 100% but you can see
0:50
most of those cores are dark, they're doing nothing they're just sitting there. That's because our code is written in a single-threaded way.
0:59
It can't go any faster. Let's just look at that in action real quick. Now, this graph, this cool graph showing the 12 CPUs and stuff
1:06
this comes from a program called Process Explorer which is a free program for Windows. I'm on my Mac, so I'll show you something
1:13
not nearly as good but I really wanted to show you this graph because I think it brings home just how underutilized this system is.
1:20
So let's go over here and we're going to run a performance or system monitoring tool actually built in Python
1:27
which is pretty cool in and of itself, called Glances. So we're sorting by CPU usage, we're showing what's going on.
1:34
We had a little Python running there, actually for a second, running probably in PyCharm which is just hanging out, but you can see the system
1:40
is not really doing a whole lot, it is recording. Camtasia's doing a lot of work to record the screen so you have to sort of factor that out.
1:47
Now, let's go over here and create another screen notice right here we have 12 cores. Okay, so if we run PtPython, which is a nicer REPL
1:58
but just Python, and we write an incredibly simple program that's going to just hammer the CPU. Say x = 1, and then while true: x += 1.
2:10
So we're just going to increment this, over and over and over. I'm going to hit go, you should see Python jump up here
2:17
and just consume 100% of one core. This column here is actually measuring in single core consumption whereas this is the overall 12 core bit.
2:32
Here it's working as hard as it can. You'll see Python'll jump up there in a second. Okay, so Python is working as hard as it can, 99.8%
2:42
running out of my brew installed Python, there. But the overall CP usage, including screen recording on this whole system?
2:51
11%, and of course as we discussed, that's because our code we wrote in the REPL, it only uses one thread, only one thread of concurrent execution
3:00
that means the best we could ever possibly get is 1/12%. 8.3% is the best we could ever do in terms of taking advantage of this computer
3:11
unless we leverage the async capabilities that we're going to discuss in this course. So if you want to take advantage of modern hardware
3:18
with multiple cores, the more cores the more demanding or more pressing this desire is you have to write concurrent multi-threaded code.
3:28
Of course, we're going to see a variety of ways to do this both for I/O bound and, like this, CPU bound work and you handle those differently in Python
3:36
which is not always true for other languages but it is true for Python. And by the end of this course, you'll know
3:42
several ways to make, maybe not this simple, simple program but to make your Python program doing real computation
3:49
push that up to nearly 100% so it's fully taking advantage of the CPU capabilities available to it.