extending python with rust
play

Extending Python with Rust Introduction and a hands-on demo of - PowerPoint PPT Presentation

Extending Python with Rust Introduction and a hands-on demo of writing Python extension in Rust This talk Why? Why Rust? Binginds Dynamic library Web service example Docker example Compile and distribute


  1. Extending Python with Rust Introduction and a hands-on demo of writing Python extension in Rust

  2. This talk Why? ● ● Why Rust? ● Binginds Dynamic library ● Web service example ● ● Docker example ● Compile and distribute (PIP example)

  3. Why? ● Speed. The dynamic nature of Python means it is notoriously slow at some things, and not great on parallel calculations. Reusability. Why port code when you can use ● it directly? ● Cooperation. If one team writes Python and another - Rust, and they need to use each other's code. Migration. If you want to re-write your entire ● Python codebase to Rust, you could do that module by module.

  4. How? C extension ● Cython - magic! ● ● Numba - JIT-compilation

  5. Why Rust? Rust is an innovative compiled language ● with an accent on safety. ● Rust is one of the most loved languages by developers. ● ...and companies too Minimal runtime (e.g. no garbage ● collector) Yet Rust has a somewhat steep learning curve. In many cases, one may want to split their codebase, and write some critical code in Rust, and some more common code in Python.

  6. Bindings Rust-cpython (which we're going to use ● for the examples) PyO3 - fork of rust-python, only on ● nightly Rust Python as a scripting language in Rust ● programs (not in this talk) Cargo - Rust package manager and CLI ● The Rust book and the Cargo book ●

  7. ● Simple Rust code - just two files Dynamic library Will be building a dynamic library - only ● need to set crate-type ● .dll file on Windows, .so on Linux, or .dylib on Mac ● Rust-cpython provides wrappers for Python ● Building for Mac requires additional linker arguments Rename to mylib.so ● ● DEMO

  8. DEMO

  9. Web service More "real-world" example ● ● A Python web-service that calls Rust library and returns the result to user ● Will use Flask + the library we just built Can run it with “FLASK_APP=main.py ● flask run” but for production, we would like more: Continuous Integration/Deployment Will build a Docker image ●

  10. Docker Will use Gunicorn as a web-server ● Will use a multi-stage build ● ● The resulting Docker image will not contain any Rust artifacts - only a compiled binary Will build for Linux this time ● ● DEMO

  11. DEMO

  12. Compile and ● What if we want to keep Rust and Python code separately? distribute ● A Rust team and a Python team ● Corporate PIP repository “pip install mylib” ● ● Setuptools-rust ● Building from source vs wheels ● Can remove the first, Rust stage from our Dockerfile ● DEMO

  13. DEMO

  14. Thanks! https://github.com/moor84

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend