Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Hello Cython
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Where else would you start with Cython other than Hello World? And for our Hello World I want to decouple things just in a little bit.
0:08
Over here we have our Hello World folder in the Cython part of our GitHub repository. And what I want to do is create some extra library
0:16
this library we're going to Cythonize going to convert it to native code through Cython. So we'll call this the greeter.
0:24
Going to have a method called greet. And what do you think it's going to do? Somethin' to this effect. And that's pretty straightforward right?
0:35
And we're going to import that over here and call it. Now right now, if I do this import greeter, and we'll say somethin' like this.
0:51
The greeter we can greet with their name. So far there's nothing to do with Cython at all. Let's run the program and see what happens.
0:59
What is your name? My name is Michael. Hello Michael, and then I printed out the response which actually is None. There we go, that looks correct.
1:11
So what I want to do is convert from using Python to Cython. Convert from using interpreted code to native code for this.
1:19
So there's two steps to do that. First thing, we're going to call out a pyx file. Notice it's got like a big C with a Python icon
1:29
right there in the middle rather than a standard file with a Python icon on it. So let's see, does it still run? No there's no greeter, not yet.
1:39
So how do we make the greeter? There's two steps. First we have to compile it and then we just use it.
1:48
In order to do the compilation we have to install Cython so pip install cython and have a setup file.
1:55
So let's go and make sure we have a requirement file. And it's going to be cython. Great, Cython is installed and we have to tell PyCharm
2:12
it's not misspelled. That's a little annoying but, no big deal. So in here we're going to write a simple little bit of code.
2:18
We're going to import setup and we're going to import from Cython some build tools so we'll say from distutils.core import setup.
2:30
It's kind of like we're creating a package like a Python package but this is just a tiny bit different. Then we say from Cython.build import cythonize.
2:41
It's all we have to do then we write or we call setup like so and we say ext_modules=cythonize() and then we give it the file name, so greeter.pyx.
2:56
Okay everything's good now we have to run this so let's go over I've already opened a terminal here with the right virtual environment activated.
3:05
So now what we have to do is, let's actually make sure our Python cache is empty as well. So what we need to do is we need to run Python setup
3:16
and then we have to give it a special command build_ext --inplace. Again this assumes you've installed into the active virtual environment, Cython.
3:28
This is a good sign, we have greeter and now if we look here we have greeter.cPython 37 on Darwin. Darwin is the platform name of macOS.
3:37
Alright let's just try this again. What is your name? Oh wait a minute, that didn't crash this time my name is Michael.
3:44
Hello Michael, how cool is that? So we're using Cython. How do I know? Well let's go over to our greeter and print somethin' like this, running from
3:55
and we'll put out the file name there. So most modules you can just say __file__ like this. Now we need to recompile.
4:04
That did not compile but that may, here we go. Now if we run it again, there's Michael. Michael and we're running from
4:13
big long thing here but notice this we're running from that compiled output right there. That is native code.
4:21
How do I know? Well let's look at the intermediate here. Notice the scroll bar. That is a lot of C a lot of C right there, wooo!
4:32
Let's go and look for Hello. Like here you can see some of the string literals that we are using that got pulled out as static constants in C.
4:42
That's pretty awesome right? So that's it. We have our temporary C output and we have this .so file in this build directory
4:49
all of that stuff is just produced output from this to run it. Source files include just our program that's going to use
4:57
the model and notice this Python code did nothing to say hold on we're doing weird Cython stuff so go import a Cython thing so just know
5:05
there's a module called greeter. We're going to import it and if we go here it has a greeter. then the auto-complete it's gone but
5:13
it has a greeter.greet okay? And we also have a setup that simply calls setup sets the ext module to cythonize
5:20
the particular Cython modules that we're going to use and that gets compiled with this command line right there. And notice if there's no changes
5:30
it sort of caches the output so it only builds what's changed. Alright well, hello Cython.