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. By looking at this concept of the no_gil operation. So in Cython, you can go and say I want to create a block of code
0:11 and in this block of the code, I'm telling Python I don't need the GIL. You can release it, during this section.
0:17 Now, if we can just say that everywhere well maybe the GIL would just go away. But as we saw, that is not allowed.
0:24 There are rules when use the no_gill. And one of them is, you cannot interact with CPython objects. So these numbers we work with
0:32 they have to be pure integers or floats. In the C world, they can't be pointers back to the actual CPython version for example.
0:42 And the place where that became a problem was we were using math.sqrt. It accepted a CPython number in which case we had to do a conversion
0:51 from our integer or float into CPython. Well, that wasn't working, right? That wouldn't compile. You cannot do a conversion into a Python object
1:01 in a no_gill block. So we had to do things like, use the libc.math and do a cimport for square root, and things like that
1:09 to make sure we're staying in C land while we're there. But if you can do that you really can break free of the GIL.
1:15 And we saw that made our code much, much faster and take advantage of the processors using pure threads in Python
1:23 which is really quite awesome, isn't it?


Talk Python's Mastodon Michael Kennedy's Mastodon