Async Techniques and Examples in Python Transcripts
Chapter: Threads
Lecture: How to choose between asyncio and threads

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

Talk Python's Mastodon Michael Kennedy's Mastodon