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