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