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