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