Async Techniques and Examples in Python Transcripts
Lecture: How to choose between asyncio and threads
0:00 Before we get into the actual programming with threads
0:03 I want to take a little step back
0:04 and help you decide which way to go
0:07 between threads and AsyncIO.
0:09 We've seen in that landscape section
0:11 that they live in the same area.
0:12 They're in the do more at once
0:14 while you're waiting on other things
0:16 rather than leverage the computational aspects
0:19 of, say, your CPU cores.
0:21 In some sense they kind of do the same thing.
0:23 I'll tell you I think the AsyncIO programming model
0:25 is actually nicer, it's cleaner.
0:29 It's basically the synchronous regular programming model
0:32 with just understanding these restartable coroutines.
0:36 But once you get that idea
0:37 it's a lot easier to do AsyncIO programming
0:39 than it is to do threaded programming.
0:42 So why are we talking about threads?
0:43 Like, why would you even care about them?
0:44 They're old school, get 'em out of here, we're done.
0:47 Well, not so fast.
0:48 We saw that you can only take advantage of AsyncIO
0:51 if you have libraries that themselves are built for AsyncIO.
0:57 Most libraries out there are not.
0:59 Sometimes you'll get lucky and you'll find some system
1:02 some library that you can talk to asynchronously
1:06 with async and await, but much of the time you won't.
1:09 There won't be anything available that you can use
1:12 but you still want to take advantage of the concurrency.
1:14 When we spoke about the GIL we said threads are no good
1:17 for concurrency when you're trying to leverage
1:19 CPU bound operations in the Python steps themselves.
1:24 And that's because the GIL will only let you execute
1:27 one operation at at time.
1:29 One of the really important caveats around that is
1:33 if you call a function that itself is, say
1:35 going over the network or talking to the file system
1:38 deep down in the implementation
1:40 Python will let go of the GIL
1:43 while it's waiting on that IO operation.
1:46 So imagine I have, let's say, SQLAlchemy Code.
1:49 I have no idea if SQLAlchemy is thread safe
1:51 if this is a good idea, but it's just an example.
1:53 So, suppose I have SQLAlchemy
1:55 which is an ORM that talks to the database.
1:56 Doesn't really have a great AsyncIO story.
2:00 It does talk to the database so I could run these queries
2:02 on multiple threads as we're waiting on the network.
2:05 It's going to release the GIL and let it keep going.
2:07 So we actually could add this AsyncIO type of programming
2:12 to systems that don't have it. So here's the take away.
2:15 AsyncIO when you can, threads when you must, all right?
2:18 If you can use AsyncIO.
2:20 Obviously it's a nice, cleaner way to do it in my opinion.
2:23 But a lot of times it's not available.
2:25 Threads are there to basically do the same thing.
2:27 You just have to construct it a little more yourself.
2:30 Hope that helps. Now, let's learn the threaded programming model.