Python for the .NET Developer Transcripts
Chapter: async and await in Python
Lecture: Python's async landscape

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's bring this all together this asynchronous landscape in Python graphically for you all. We can have basically a divide into two things.
0:10 Can we do more at once, that is like while we're waiting can we go do other stuff, basically or can we actually do them faster by taking advantage
0:19 of those multiple cores? On the do more at once side, we have Async IO and the async and await keywords, so this is really about IO driven concurrency
0:29 basically what we're going to dig in to. We also could do threads. Because of the Global Interpreter Lock the only time threads really add concurrency
0:37 is when the thread, one of the threads releases the GIL. This is if you put a thread to sleep if you talk over the network
0:45 there's certain types of things that will trigger that to happen, but pure computational stuff just running in parallel across all the cores
0:52 like you can do in C# with a task that's not something you can do in Python. It doesn't work that way. There have been many attempts to remove the GIL.
1:00 There are new attempts which actually look promising. The previous ones didn't go anywhere. There's some new work being done that actually might
1:07 bring Python threads on par with C# and .NET but for the moment, this is the world it lives in. To do stuff faster, to get around the GIL
1:15 we have to not use threads, but multiprocessing which have a very similar API, it's not nearly as much work
1:21 as it might sound, but that is what we got to use or we have to write in C. We could write C extensions
1:27 or even we can write in something called Cython. You write in Python, with type hints but then it actually compiles to C.
1:34 Cython has some keywords that are like using statements basically, that let you say this part I'm going to release the GIL and go crazy
1:41 and you can get really good parallel performance by still more or less writing Python code but making certain parts run or be implemented in Cython.
1:50 Now there's a couple libraries that bring this stuff together to make it easier to not see all these differences.
1:56 One is Trio. It's a kind of a task scheduling library with cancellation and stuff. That's also quite similar to .NET and the built-in things there
2:05 and then there's something called Unsync which is a beautiful, beautiful API on top of Async IO threads and multiprocessing.
2:12 We'll actually look at that just briefly at the end. It was inspired by C#, as we'll talk about when we get to it. So this is landscape, keep it live.


Talk Python's Mastodon Michael Kennedy's Mastodon