Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Fast threading with Cython (conversion)
0:00 Alright to make our math go faster we're going to go
0:03 First let's go to the file we have named over here.
0:05 Now this is an exact copy of the threaded version.
0:08 Remember the one that's barely better at all?
0:10 And it has this math part here
0:12 but let's take this and put it somewhere else.
0:19 I'll call it math_core, something silly like that, pyx.
0:23 And in here we're going to put that
0:25 and in order to make this work, we have to import math.
0:28 So let's comment that out and come over here and say...
0:34 We're going to import math_core
0:36 and we'll do math a couple times.
0:42 Let's run this one.
0:43 Now we've done nothing with Cython.
0:44 Actually it's not going to find it.
0:46 It's just going to crash, there's no math_core.
0:48 It would run the same speed as the threaded one
0:49 if we hadn't compiled it, right?
0:51 So this is not anything to do
0:53 with converting it to Cython yet.
0:55 This is just restructuring it
0:57 so it's actually in a Cython file
1:00 but we're not changing the syntax
1:01 to really take advantage of it.
1:03 So let's just rob from what we did before.
1:05 We need a setup file, we know about that.
1:07 So when I go over here
1:09 And now we need a math_core, we know about that.
1:13 And I've changed directory over here
1:15 so now we can say, "Python, build and place again."
1:19 Now, Now if I run it.
1:21 We're going to run this on Cython, if it runs at all.
1:25 I told you Cython is awesome
1:27 cause it breaks free from the GIL.
1:29 So this may go faster, actually dunno.
1:31 I have no idea what's going to happen here when I run this.
1:33 I think it will work. I don't know how much faster it will be.
1:35 Let's find out. Huh, well it's faster that's cool.
1:41 Remember we had a 1.07 factor.
1:44 a 1.07 factor for the Python-threaded version.
1:48 And now we got this version here that's faster
1:51 but not that fast, why?
1:54 Well, the GIL is actually still operating here.
1:58 The GIL is still in place. So what we need to do is
2:01 we're going to go and apply a technique
2:03 that will explicitly factor our code
2:05 into the part where the GIL is required
2:07 and a part where there's no GIL required
2:08 cause it's effectively C.
2:10 If we indicate that using a Cython syntax
2:13 well, then we're going to break free from this blockade
2:16 that we have because of the GIL.