Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Fast threading with Cython (GIL-less)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're over here in our Cython file and we can see that it didn't speed things up that much. And I said that's because of the GIL.
0:08 If we could just run this part in parallel like, that's where almost all the work is. Things would be golden. So let's try that.
0:14 So we can come over here and there's a cool syntax in a context manager. We can say with no_gil:. Is that all it took? Like, are we free of the GIL?
0:21 We just got to say no_gil? Not so much. Let's save this and then recompile it down here. Darn it, thought it was going to be so easy.
0:30 Operation not allowed without the GIL on line 9, column 42. Line 9, column 42. So what are the rules with this no_gil?
0:39 Well, the rules are you cannot interact with Python objects. You can convert them down and then use them in Cython
0:47 but once you get to the no_gil section it's got to be all C. And right now that's a Python number type. So what we need to do is say, no, no, no
0:56 this is a cython.int. We can import Cython up there. Now in pyterm it gives you this little warning but just ignore it.
1:06 I believe we can get away with ignoring it. Got that, and again, up here, we got to do that for all of these values. We're good. Nope, we're not good.
1:14 Cause I have that backwards for my default. There we go, now we're good. So we still have these little warnings but let's see what happens now.
1:28 Error. Converting to Python object without a GIL. And where is this happening? 1235. Oh, I made a mistake in the import. That's what's not working.
1:44 So, a couple things. This is not regular import, this is cimport a Cython thing, and we're going to say .math. OK. Now, that should make it work.
1:55 Look at that, it compiled, and now we have a math_core a new one, and all of this work which is where almost all the work is
2:04 is now going to operate without the GIL. Let's try it one more time. Remember the speed-up we got, maybe it's still here.
2:12 It is, perfect, 2.5 times speed-up. Can we do better? Give it a shot. Look at that. Oh my gosh. 5,596 times faster. Not percent, times.
2:33 Now, I want to caution you about one thing really quickly here. You might think, "Alright, what we can do is I can just treat this like Python."
2:42 And in Python you don't think about numbers, very much. If we go over to our do_math, right you can keep making integers bigger
2:49 and bigger, and bigger, and they can basically be as large as you have memory and patience. But, in Cython, these are C integers.
2:57 Remember C integers are just 32 bit things. They overflow really easily. So if you tweak these number around too much
3:05 let's actually take this number down a little bit just to make sure we're not overflowing the available numbers here.
3:11 And I'll compare it against the serial one with the same amount, what do we say, 30,000? 300,000. So let's run it one more time here.
3:22 .09. There we go, that's more like it. So 56 times faster. I think we might have actually been overflowing that integer, which is not great
3:37 and then when you do this is less than that all the sudden you increment it enough to overflow it and then it swaps around, right?
3:43 If you're not familiar, if you overflow an integer it basically becomes negative, right? The next time you add a number to it
3:50 add a one to it, instead of just getting bigger it flips to its smallest possible value which is like negative, I don't know, 65,365
3:57 or who knows what this turns out to be. OK, so this should be a little more accurate here. What you see, it's still a massive, massive speed-up.


Talk Python's Mastodon Michael Kennedy's Mastodon