Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Why Cython
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, okay I hear you. You either don't want to write C, you would rather avoid writing C, or you don't even know C.
0:07
Great, you can still write really awesome code with your Python knowledge using something called Cython.
0:14
Now, Python officially is known as its internal runtime what's called the interpreter sometimes is known as CPython.
0:22
That's because the interpreter, the runtime is implemented in C but you take regular Python code and it is interpreted on top of it.
0:30
Cython is unrelated to CPython even though they differ by only one letter. Cython is an optimizing static compiler for the Python programming language.
0:39
And what does it compile to? Well, it first compiles to C and then C compiles to actual machine instructions for your operating system.
0:47
It's as if you had written C code but the syntax you had to write and the libraries you get to use are Python.
0:54
It's beautiful and it makes writing C extensions for Python as easy as Python itself and you can bet it actually has some
1:02
threading implications along the way. So, why would we do this? Well, writing C, we already talked about some
1:07
of the advantages there but calling them out explicitly we have, you can write Python code that at any point
1:15
can call back and forth between native C or C++ code. So if you want to do any integration with other C code
1:22
or bring that into your system, guess what? You can do that in Python back and forth. Not with some weird integration layer
1:29
but just as if you were using that library directly. You could easily take Python code and convert it to high performance C code by just changing it
1:38
a little bit by adding static type declarations. You get integrated debugging, so you can debug from
1:44
Python to Cython to C and back. That's pretty awesome. You can interact effectively with large data sets. Cython actually comes from the computational
1:53
numerical analysis side of Python and the mathematical spaces and so on. So it has good support with things like
1:59
Numpy which also, as I said, has big parts of it in C. So why do you need to filter all of that data through Python
2:07
to get it over to the Numpy C stuff? Just work directly here and you can get some really good performance.
2:12
Because Cython can interact with the libraries from CPython, you can pip install some random library and use that.
2:20
All of the magic and power of CPython's ecosystem is available to your what is effectively C code in Cython. So that's pretty awesome.
2:30
You want to use requests from Cython, no problem. Finally, you can integrate natively with existing code.
2:36
Is there some C library that you would like to use but you want to write in Python and how do you link those together?
2:43
Or do you have some big application that is written in C? Well this is a really easy way to bring that into Python. So these reasons and more are why
2:51
you might be interested in Cython. There's a lot to it but simple use cases pretty simple and we're going to get some really cool
2:58
performance outcomes from it.