Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Concept: Cython's nogil

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Close out our chapter on Cython.
0:01 By looking at this concept of the no_gil operation.
0:05 So in Cython, you can go and say
0:08 I want to create a block of code
0:10 and in this block of the code, I'm telling Python
0:13 I don't need the GIL.
0:14 You can release it, during this section.
0:16 Now, if we can just say that everywhere
0:18 well maybe the GIL would just go away.
0:20 But as we saw, that is not allowed.
0:23 There are rules when use the no_gill.
0:25 And one of them is, you cannot interact
0:27 with CPython objects.
0:29 So these numbers we work with
0:31 they have to be pure integers or floats.
0:35 In the C world, they can't be pointers
0:37 back to the actual CPython version for example.
0:41 And the place where that became a problem was
0:44 we were using math.sqrt.
0:46 It accepted a CPython number
0:48 in which case we had to do a conversion
0:50 from our integer or float into CPython.
0:52 Well, that wasn't working, right?
0:55 That wouldn't compile.
0:57 You cannot do a conversion into a Python object
1:00 in a no_gill block.
1:02 So we had to do things like, use the libc.math
1:06 and do a cimport for square root, and things like that
1:08 to make sure we're staying in C land while we're there.
1:12 But if you can do that
1:13 you really can break free of the GIL.
1:14 And we saw that made our code much, much faster
1:17 and take advantage of the processors
1:19 using pure threads in Python
1:22 which is really quite awesome, isn't it?