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

extending python with rust
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Extending Python with Rust

Introduction and a hands-on demo of writing Python extension in Rust

slide-2
SLIDE 2

This talk

  • Why?
  • Why Rust?
  • Binginds
  • Dynamic library
  • Web service example
  • Docker example
  • Compile and distribute (PIP example)
slide-3
SLIDE 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

  • ther's code.
  • Migration. If you want to re-write your entire

Python codebase to Rust, you could do that module by module.

slide-4
SLIDE 4

How?

  • C extension
  • Cython - magic!
  • Numba - JIT-compilation
slide-5
SLIDE 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.

slide-6
SLIDE 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
slide-7
SLIDE 7

Dynamic library

  • Simple Rust code - just two files
  • Will be building a dynamic library - only

need to set crate-type

  • .dll file on Windows, .so on Linux, or .dylib
  • n Mac
  • Rust-cpython provides wrappers for

Python

  • Building for Mac requires additional

linker arguments

  • Rename to mylib.so
  • DEMO
slide-8
SLIDE 8

DEMO

slide-9
SLIDE 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
slide-10
SLIDE 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
slide-11
SLIDE 11

DEMO

slide-12
SLIDE 12

Compile and distribute

  • What if we want to keep Rust and Python

code separately?

  • 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
slide-13
SLIDE 13

DEMO

slide-14
SLIDE 14

Thanks!

https://github.com/moor84