Async Techniques and Examples in Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Cython

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We say that Python threads do not really
0:03 add any benefit whatsoever if they're CPU bound
0:06 so if most of their operations are actually Python
0:08 instructions, not just waiting on a web service
0:11 call or something on the network or file system
0:13 that's because of the GIL.
0:15 The GIL means in this Python process only one interpreter or
0:19 instruction will execute at a time, it doesn't matter how
0:22 many threads there are, which one's running one at a time.
0:25 Okay, so that's the GIL.
0:27 We saw that if it's Cython, we can rewrite our method in C
0:32 because Cython takes Python and compiles it to C.
0:35 And we can also use a nogil keyword which tells
0:39 CPython, everything we're doing here has nothing
0:41 to do with Python, it's pure C
0:44 so it's just going to operate down here.
0:46 You go let other stuff run and when
0:47 this comes back we'll reacquire the GIL.
0:49 So all you have to do is say with gil and then
0:52 you do your computational work.
0:54 It sounds simpler than it is though because
0:56 you have to make sure that you're not interacting
0:58 with any Python objects, you're going to capture
1:01 all those values and then use them basically in C.
1:04 And that's even down to the square root method.
1:06 So we were using math.sqrt, but that was a
1:09 Python method so it accepted a py object type thing
1:13 and that was going to work in the nogil world.
1:15 So we had to use the libc.math sqrt
1:19 and remember we used cimport, not regular import there.
1:22 But once we did this, I saw like 1,000 times
1:26 improvement in our code, it was really, really
1:29 worth it for what we were doing.
1:31 So Cython wouldn't be the first thing I jump at
1:33 but if its solution fits your use case
1:36 it seems like a really good option.