Skip to content

Commit 8b8bbd9

Browse files
committed
Transcripts complete for async course.
1 parent ca9dda4 commit 8b8bbd9

23 files changed

+948
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
00:00 Hello and welcome to Asynchronus
2+
00:02 Techniques and Examples in Python.
3+
00:04 In this course, we're going to explore the entire
4+
00:06 spectrum of asynchronous programming in Python.
5+
00:09 This means threads, this means subprocesses
6+
00:13 the fancy new asyncio and the async and await keywords.
7+
00:17 All of that and much, much more.
8+
00:19 I hope you're excited about digging into it
9+
00:21 because I'm super excited to share it with you.
10+
00:24 Now before we get into the details of what we're going to
11+
00:25 cover, let's talk really briefly about
12+
00:27 what asynchronous programming is.
13+
00:31 If we go and look at Wikipedia it'll say
14+
00:33 asynchrony and computer programming refers the
15+
00:35 occurrence of events independent of the main
16+
00:37 program flow and way of dealing with such events.
17+
00:41 These may be outside events such as the arrival
18+
00:44 of signals or actions started by the program
19+
00:46 without blocking or waiting for results.
20+
00:49 Now what's really important about this statement
21+
00:51 is it doesn't say asynchronous programming is
22+
00:54 you create threads and then you join on them
23+
00:57 or you create other processes and wait for
24+
00:59 them to finish or get back to you.
25+
01:01 It just says stuff happening at the same time.
26+
01:05 So we're going to look at many different ways
27+
01:07 in fact we're going to explore three very
28+
01:09 different approaches that Python can
29+
01:11 take to this: threads, processes, and asyncio.
30+
01:16 We're going to explore more than that of course.
31+
01:18 But there's actually a lot of ways in which
32+
01:19 asynchrony can be achieved in Python and knowing
33+
01:23 when to choose one over the other is super important.
34+
01:25 We're going to talk all about that.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
00:00 Here's a graph we're going to come back to
2+
00:01 and analyze at great depth later in the course.
3+
00:05 It came from a presentation by Jeffrey Funk.
4+
00:07 You can see the SlideShare link there
5+
00:09 if you want to go check it out.
6+
00:10 It's actually 172 slides.
7+
00:12 I want to just call your attention to this graph around 2005.
8+
00:16 If you look at, most importantly, the dark blue line
9+
00:20 that is single-threaded performance over time.
10+
00:24 Note, it's going up and up and up
11+
00:25 following Moore's law, that's the top line
12+
00:27 and then it flattens out
13+
00:28 and it actually is trending downward.
14+
00:31 What is going on here?
15+
00:32 Well, look at the black line.
16+
00:34 We have the number of cores
17+
00:35 going from one up to many
18+
00:38 right around 2005 and continuing on today.
19+
00:41 To take full advantage of modern hardware
20+
00:44 you have to target more than one CPU core.
21+
00:48 The only way to target more than one CPU core
22+
00:50 is to do stuff in parallel.
23+
00:52 If we write a regular while loop
24+
00:54 or some sort of computational thing in regular Python
25+
00:57 that is serial and it's only going to run on one core
26+
00:59 and that means it's following that blue downward line.
27+
01:02 But if we can follow the number
28+
01:04 of cores growing, well, we can multiply
29+
01:05 that performance massively, as we'll see.
30+
01:09 One of the reasons you care about asynchronous programming
31+
01:12 is if you have anything computational to do
32+
01:15 that depends on getting done fast
33+
01:17 not like I'm calling a database
34+
01:19 or I'm calling a web service and I'm waiting.
35+
01:20 That's a different type of situation we'll address.
36+
01:23 But no, I have this math problem
37+
01:25 or this data analysis problem
38+
01:26 and I want to do it as fast as possible in modern hardware.
39+
01:29 You're going to see some of the techniques
40+
01:31 that we talk about in this course
41+
01:32 allow us to target the new modern hardware
42+
01:35 with as much concurrency as needed
43+
01:37 to take full advantage of all the cores of that hardware.
44+
01:40 So like I said, we're going to dig way more into this later
45+
01:43 I just want to set the stage that if you have
46+
01:45 anything computational and you want to take full advantage
47+
01:48 of modern hardware, you need asynchronous programming.
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
00:00 Let's talk about the actual topics
2+
00:02 that we're going to cover chapter by chapter
3+
00:04 and how it all fits together.
4+
00:06 We're going to start digging further into why do we care
5+
00:09 about async, when should we use it, what are its benefits.
6+
00:12 So we're going to go way into it so you understand
7+
00:14 all the ways in which we might use asynchronous programming
8+
00:16 in Python, and when you might want to do that.
9+
00:19 Then it's time to start writing some code
10+
00:21 and making things concrete.
11+
00:23 We're going to focus first on the new key words introduced
12+
00:26 in Python 3.5 async and await.
13+
00:29 Now some courses leave this to the end
14+
00:31 as the great build-up, but I think you start here.
15+
00:34 This is the new, powerful way to do threading
16+
00:37 for anything that is waiting.
17+
00:39 Are you calling it database?
18+
00:41 Are you talking to a web service?
19+
00:42 Are you talking to the file system, things like that?
20+
00:45 We do these kinds of things all the time in Python
21+
00:47 and it' really not productive to just block our program
22+
00:50 while it's happening. We could be doing many other things.
23+
00:52 And the async and await key words in the asyncio foundation
24+
00:57 make this super straightforward.
25+
00:59 It's almost exactly the same programming model
26+
01:01 as the serial version
27+
01:03 but it's way more scalable and productive.
28+
01:06 Next we're going to focus on threads, sticking
29+
01:08 to making a single process more concurrent
30+
01:11 doing more at once.
31+
01:12 We're going to talk about a more traditional way
32+
01:15 of writing asynchronous code in Python with threads.
33+
01:18 We'll see sometimes this is super-productive.
34+
01:21 other times it's not as productive as you might hope
35+
01:24 especially because of things like the GIL raise its head.
36+
01:27 And we'll see when and how to deal with that.
37+
01:29 Some things are well-addressed with threads
38+
01:31 others not so much.
39+
01:33 When we start writing multithreaded code
40+
01:35 or asynchronous code, in general
41+
01:37 we have to think very carefully
42+
01:38 about the data structures that we use
43+
01:41 and making sure that we don't encourage a race conditions
44+
01:43 or deadlocks.
45+
01:44 So in this chapter we're going to talk about both of those.
46+
01:48 How do we prevent race conditions that might allow code
47+
01:51 to see invalid data or corrupt our data structures?
48+
01:53 And how do we prevent deadlocks
49+
01:55 from completely freezing up our program
50+
01:57 by the improper use of the tools trying
51+
01:59 to prevent the first?
52+
02:01 So we're going to talk a lot about thread safety
53+
02:02 make sure you get that just right.
54+
02:04 Now Python has two traditional types of parallelism
55+
02:07 threaded parallelism and process-based parallelism.
56+
02:10 And the primary reason we have this is because of the GIL.
57+
02:14 We'll see that threaded-based parallelism
58+
02:16 is great when you're waiting on things like databases
59+
02:18 or web calls, things like that.
60+
02:20 But it's basically useless for computational work.
61+
02:24 So if you wanta do something computational
62+
02:27 we're going to have to employ process-based parallelism.
63+
02:30 We're going to talk about Python's native, multiprocessing
64+
02:33 process-based parallelism, with tools all around
65+
02:37 that meant to take a bunch of work
66+
02:38 and spread it across processes.
67+
02:40 You'll see that the API for working with threads and the API
68+
02:43 for working with processes are not the same.
69+
02:46 But the execution pools are ways to unify these things
70+
02:50 so that our actual algorithms or actual code depend
71+
02:53 as little as possible on the APIs
72+
02:55 for either threads or processes, meaning we can switch
73+
02:58 between threads or processes depending on what we're doing.
74+
03:01 So we wanta talk about execution pools
75+
03:03 and how to unify those two APIs.
76+
03:07 Then we're going to see two really interesting libraries
77+
03:11 that take async and await and asyncio and make it better
78+
03:15 make it easier to fall into the pit of success.
79+
03:18 You just do the right thing, and it just happens.
80+
03:21 The way it guides you, things work better.
81+
03:23 So things like cancellation, parent/child tasks
82+
03:26 or any mix mode of, say, some IO-boundwork
83+
03:30 and some CPU boundwork.
84+
03:32 That can be really tricky, and we'll see some libraries
85+
03:34 that make it absolutely straightforward and obvious.
86+
03:37 One of the great places we would like
87+
03:39 to ply asyncio is on the web.
88+
03:42 That's a place where we're waiting on databases
89+
03:44 and other web services all the time.
90+
03:46 We'll see the traditional, popular frameworks
91+
03:49 like Django, Flask, Pyramid do not support any form
92+
03:53 of asynchrony on the web.
93+
03:55 So we'll take something that is a Flask-like API
94+
03:58 and adapt it to use asyncio
95+
04:00 and it's going to be really, really great.
96+
04:01 We'll see massive performance improvements
97+
04:03 around our web app there.
98+
04:05 Finally, we'll see that we can integrate C with Python
99+
04:09 and, as you know, C can do just about anything.
100+
04:12 Your operating system is written in C.
101+
04:14 It can do whatever it wants.
102+
04:16 So we'll see that C is actually a gateway
103+
04:18 to a different aspect, different type
104+
04:21 of parallelism and performance in Python.
105+
04:23 But we don't wanta write C.
106+
04:25 Maybe you do, but most people don't want to write C
107+
04:27 if they're already writing Python.
108+
04:28 So we'll see that we can use something called Cython
109+
04:31 to bridge the gap between C and Python
110+
04:33 and Cython has special key words to unlock C's parallelism
111+
04:38 in the Python interpreter.
112+
04:40 It's going to be great.
113+
04:41 So this is what we're covering during this course
114+
04:43 and I think it covers the gamut
115+
04:45 of what Python has to offer for asynchronous programming.
116+
04:49 There's so much here; I can't wait
117+
04:51 to get started sharin' it with you.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
00:00 Let's take a moment and just talk really quickly
2+
00:02 about the prerequisites for this course.
3+
00:05 What do we assume you know?
4+
00:06 Well, we basically assume that you know Python.
5+
00:09 This course is kind of intermediate to advanced course
6+
00:13 I would say, it's definitely not a beginner course.
7+
00:15 We run through all sorts of Python constructs
8+
00:17 classes, functions, keyword arguments, things like that
9+
00:21 without really explaining them at all.
10+
00:23 So if you don't know the Python language
11+
00:25 you're going to find this course a little bit tough.
12+
00:27 I recommend you take my Python Jumpstart
13+
00:29 by Building 10 Apps, and then come back
14+
00:32 and pick this course up.
15+
00:33 You don't have to be an absolute expert in Python
16+
00:36 but like I said, if you're brand new to the language
17+
00:39 take a foundational course first
18+
00:40 and then come back and dig in
19+
00:41 to the Asynchronous Programming after that.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
00:00 Finally in this chapter, I just want to say hello
2+
00:02 and thank you for taking my course.
3+
00:04 My name is Michael Kennedy, find me on Twitter @mkennedy.
4+
00:08 You may know me from The Talk Python to Me podcast
5+
00:10 or the Python Bytes podcast.
6+
00:12 I am the host or co-host on both of those
7+
00:16 and each of them cover different aspects
8+
00:18 of asynchronous programming, so for example
9+
00:20 I've had Nathaniel on to talk about Trio
10+
00:22 which is one of the libraries we're going to cover.
11+
00:24 Philip Jones to talk about Quart
12+
00:26 another thing we're going to use in this course
13+
00:27 on the Talk Python to Me podcast.
14+
00:29 You can listen to those interviews.
15+
00:30 And on the Python Bytes, we highlight all sorts of libraries
16+
00:33 that are really useful for asynchronous programming.
17+
00:37 So make sure to check those out if you're interested
18+
00:38 and I'm also the founder of Talk Python Training.
19+
00:42 Welcome to my course, it's great to meet you
20+
00:44 and I'm looking forward to helping you
21+
00:46 demystify asynchronous programming.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
00:00 So we converted from asyncio to Trio
2+
00:03 and we saw things got simpler this async with block.
3+
00:06 The nursery concept is quite simple.
4+
00:08 Let me show you how we can use that
5+
00:10 for even greater benefit.
6+
00:11 I talked about cancellation. Now watch this.
7+
00:14 Let's suppose we want this to run for either
8+
00:17 five seconds and finish successfully
9+
00:19 or if it goes past five seconds
10+
00:20 we're going to cancel all the work.
11+
00:22 OK, even all the child tasks that maybe
12+
00:24 those processes themselves kicked off.
13+
00:27 How'd we do that? It sounds complicated, right?
14+
00:30 Watch this. With trio.move_on_after, let's say 5.
15+
00:36 How about that? Let's see what happens here.
16+
00:39 It should run for a little bit.
17+
00:40 Do some generating, do some producing, some consuming
18+
00:43 and then it should cancel.
19+
00:45 So this line should print out in just about five seconds.
20+
00:48 Let's find out what happens, it's working, and it's working.
21+
00:52 Five seconds are past, and boom. We ran out of time. We're done.
22+
00:57 We canceled them, straight away. How cool is that?
23+
01:01 So if we make that less work, two, two, and four
24+
01:04 something like that, should be able to produce
25+
01:06 all the work and just finish early.
26+
01:08 No cancellation, 2.42 seconds.
27+
01:11 But if it takes too long, this cancellation
28+
01:13 kicks in and off it goes.
29+
01:15 So these interesting coordination concepts
30+
01:18 like to have this block only run for so long
31+
01:21 with some sort of time out, also if this thing kicks off
32+
01:25 child tasks and then you decide to cancel it or time it out
33+
01:29 or something like that or this one has an error.
34+
01:32 Even those get canceled if they're still running.
35+
01:34 There's really interesting stuff that happens
36+
01:36 around this behind the scenes that does happen
37+
01:39 in normal asyncio, definitely doesn't happen
38+
01:41 in threading or multiprocessing that makes this
39+
01:44 a really nice coordination framework.
40+
01:46 And that's Trio, like I said
41+
01:47 there's actually a lot more to it.
42+
01:49 You can build really interesting things from the ground up.
43+
01:51 But I think this is enough to give you
44+
01:53 some inspiration and some ideas to go explore it
45+
01:55 if it's useful for you.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
00:00 Let's review our core concepts around Trio.
2+
00:02 Trio is independent async await capable
3+
00:07 library that is entirely independent
4+
00:09 of Python's built in asyncio library.
5+
00:12 That whole loop of structure and
6+
00:14 underlying things has nothing to do with Trio.
7+
00:17 So Trio is it's own thing from scratch.
8+
00:19 And the reason it's like that is they want to make
9+
00:22 sure they have complete control over how stuff
10+
00:25 executes, the cancellation, the error handling
11+
00:26 all of those types of things.
12+
00:29 So here's a general use case, I'd like to go an generate
13+
00:32 some work and consume some work with the
14+
00:34 producer consumer style and I want to maybe even
15+
00:37 let those pieces kick off sub child tasks and so on.
16+
00:41 And I'd like to either do all the work and finish
17+
00:43 successfully in five seconds or I want them
18+
00:46 to be canceled if they have still pending work.
19+
00:49 So we just create one with block for the move on part
20+
00:51 that's the time out and then the async with block.
21+
00:55 One's async, one is not, be careful there.
22+
00:58 The async with block to open the nursery
23+
01:00 kick off all the work and either that's going to block
24+
01:02 until it's finished, it's going to block until
25+
01:04 you cancel or it's going to block until one of them has an
26+
01:06 error in which case it'll cancel all the
27+
01:08 running and not yet started tasks.
28+
01:12 It's a really simple coordination
29+
01:13 concept around async work.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
00:00 Before we move on from Trio, I just want to point out
2+
00:02 Trio-async, and I did throw this out at the beginning
3+
00:05 when we talked about Trio, but I want to make it super clear
4+
00:08 if for example, you want to write in Trio
5+
00:10 but somewhere you want to use, say, aiohttp client
6+
00:14 we've seen how awesome that is
7+
00:16 and that's a totally reasonable thing to integrate.
8+
00:18 Or maybe I want to use the async library
9+
00:20 that talks to Postgres.
10+
00:21 Well, for sure, the aiohttp client will not work
11+
00:25 inside of Trio's async methods.
12+
00:28 You'll get some kind of error that it's not
13+
00:30 the right kind of event loop.
14+
00:31 It's not an asyncio event loop, it's a Trio event loop
15+
00:33 and they can't deal with each other.
16+
00:35 If you want to use those libraries in code
17+
00:38 that you write based on Trio, you have to use this library
18+
00:42 to create adapting a layer of asyncio event loop
19+
00:46 over the Trio's event loop.
20+
00:48 Just be aware that you're going to this if you want to use
21+
00:51 any external libraries that are built on top of asyncio.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
00:00 The next extra library that we're going to
2+
00:03 look at that builds on asyncio and makes it better
3+
00:06 and easier and simpler is Trio.
4+
00:08 Trio's goal is to produce a production quality, permissively
5+
00:11 licensed async and await native io library for Python.
6+
00:15 So it has a bunch of things for work with networks
7+
00:20 and so on, but it also has really great support for just
8+
00:21 coordinating anything built on top of asyncio itself.
9+
00:25 So where Trio really shines is it tries to focus
10+
00:28 on making coordination around asyncio simpler.
11+
00:32 They want to make it easy to do the best right thing
12+
00:35 with asyncio, so what's interesting is this is a framework
13+
00:38 built entirely from the ground up that integrates
14+
00:40 with async and await, but does not directly use asyncio.
15+
00:45 So like asyncio.get_event_loop, that is not part of this whole
16+
00:51 framework, it's something similar and in parallel to that
17+
00:54 and you actually have to use a bridging library.
18+
00:57 Something called trio-asyncio in order to actually
19+
01:00 use things built on the asyncio event loop.
20+
01:04 I think you'll find Trio an interesting
21+
01:06 library that could work for certain circumstances
22+
01:09 and types of apps you're building.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
00:00 Our goal with Trio here is
2+
00:01 to take something we've already done
3+
00:04 and make it simpler
4+
00:05 and actually make it do even more
5+
00:07 without any complications at all.
6+
00:10 You guys remember the producer-consumer,
7+
00:12 here we are again.
8+
00:13 It's a simple application
9+
00:14 and there's a really nice example
10+
00:16 I can show you here with Trio.
11+
00:18 So, here's the synchronous version, we've seen that.
12+
00:21 We've taken the synchronous version
13+
00:22 and we've upgraded it to use asynch I/O.
14+
00:25 So, we get the event loop.
15+
00:27 We create all the tasks.
16+
00:29 We turn them into a single task.
17+
00:30 We wait for them to run.
18+
00:31 This is okay.
19+
00:32 One of the things we didn't really talk about
20+
00:34 is cancellation.
21+
00:35 What if we're going to say,
22+
00:36 run this and if the user chooses to cancel,
23+
00:39 or we're willing to wait,
24+
00:40 say up to five seconds but not longer,
25+
00:42 then we're going to bail
26+
00:44 and cancel all the tasks and things like that.
27+
00:45 So, we'll see that with Trio
28+
00:47 this more advanced scenario becomes super easy.
29+
00:49 But our first job is we're going to take this app
30+
00:52 and we're going to convert it to use Trio,
31+
00:54 and a little bit like unsync,
32+
00:55 you're going to see that some of the challenges
33+
00:58 and ugly hoops you have to jump through just go away.

0 commit comments

Comments
 (0)