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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright to make our math go faster we're going to go First let's go to the file we have named over here.
0:06 Now this is an exact copy of the threaded version. Remember the one that's barely better at all? And it has this math part here
0:13 but let's take this and put it somewhere else. I'll call it math_core, something silly like that, pyx. And in here we're going to put that
0:26 and in order to make this work, we have to import math. So let's comment that out and come over here and say... We're going to import math_core
0:37 and we'll do math a couple times. Let's run this one. Now we've done nothing with Cython. Actually it's not going to find it.
0:47 It's just going to crash, there's no math_core. It would run the same speed as the threaded one if we hadn't compiled it, right?
0:52 So this is not anything to do with converting it to Cython yet. This is just restructuring it so it's actually in a Cython file
1:01 but we're not changing the syntax to really take advantage of it. So let's just rob from what we did before. We need a setup file, we know about that.
1:08 So when I go over here And now we need a math_core, we know about that. And I've changed directory over here
1:16 so now we can say, "Python, build and place again." Now, Now if I run it. We're going to run this on Cython, if it runs at all.
1:26 I told you Cython is awesome cause it breaks free from the GIL. So this may go faster, actually dunno.
1:32 I have no idea what's going to happen here when I run this. I think it will work. I don't know how much faster it will be.
1:36 Let's find out. Huh, well it's faster that's cool. Remember we had a 1.07 factor. a 1.07 factor for the Python-threaded version.
1:49 And now we got this version here that's faster but not that fast, why? Well, the GIL is actually still operating here.
1:59 The GIL is still in place. So what we need to do is we're going to go and apply a technique that will explicitly factor our code
2:06 into the part where the GIL is required and a part where there's no GIL required cause it's effectively C. If we indicate that using a Cython syntax
2:14 well, then we're going to break free from this blockade that we have because of the GIL.


Talk Python's Mastodon Michael Kennedy's Mastodon