Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁

Overview

  1. It is not preferable in terms of speed to write the algorithm in Pyhton ~
  2. Alright, look for something written around C, C ++ and call it from Python to speed it up.
  3. I can't find a good library,
  4. Oh, if it was written in a language called Rust
  5. Can Rust be called from Python? ??

This is Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀

It will be a continuation of.

This goal

This time, --Added lib.rs --Add setup.py and actually compile

I will continue to do it.

The ultimate goal is Being able to easily call functions and classes (like things?) Written in rust from python is.

Pass Rust functions to Python


//lib.Continuation of rs
use pyo3::{wrap_pyfunction};

// ======================CREATING MODULE FOR PYTHON==================================================
/// This module is a python module implemented in Rust.
#[pymodule]
fn test_library(py: Python, m: &PyModule) -> PyResult<()> {
    m.add_wrapped(wrap_pyfunction!(get_prime_numbers))?;

    Ok(())
}

here, #[pymodule]Decoratortest_libraryDeclares to be a python module.

And the previous get_prime_numbers function,


m.add_wrapped(wrap_pyfunction!(get_prime_numbers))?;

Wrapped as a function in that module.

Finally, it is ʻOk (())`, and this module is defined as a function,

PyResult<()> 

I'm a little worried that it returns an empty PyResult (the Void function can be wrapped with this), I wrote the tutorial as it is, assuming that this area is magical.

Create setup.py

We will create the familiar setup.py that was also used in Cython. This time it's simple and easy.

setup.py


from setuptools import setup
from setuptools_rust import Binding, RustExtension

setup(name='test_library',
        version='0.1',
        rust_extensions=[
            RustExtension('test_library', 'Cargo.toml',
                binding=Binding.PyO3)],
            zip_safe=False)
name='test_library',

Can be called from the Python side as ʻimport test_library`.

RustExtension('test_library', 'Cargo.toml',
                binding=Binding.PyO3)],

Libraryize the test_library module in lib.rs with pyo3. At this time, it is very easy to write the dependency to be used as Cargo.toml. ..

Try to build

Now that we're ready, let's actually build and call the function from Python.


python setup.py install

And test code

test.py


import test_library 
import time
import sys

def get_prime_numbers(n: int):
    flags = [True for _ in range(n+2)]

    upper = int(n ** 0.5)
    for i in range(2, upper+1):
        if not flags[i]:
            continue

        prime = i

        j = prime * 2
        while j <= n:
            flags[j] = False
            j += prime

    primes = []
    for i in range(2, n+1):
        if flags[i]:
            primes.append(i)

    return primes

if __name__  == "__main__":
    # just calling rust function from created library
    a = 123
    b = 456
    c = test_library.sum_as_string(a, b)

    print(c)


    
    # just calling rust function from created library, to find primes
    # rust is (of course) a lot faster than python
    # if you wanna call python function, python test.py, 
    # if you wanna call rust   function, python test.py --rust
    
    use_rust = len(sys.argv) == 2 and sys.argv[1] == "--rust"
    n = 10000


    t1 = time.time()
    for _ in range(10):
        if use_rust:
            primes = test_library.get_prime_numbers(n)
        else:
            primes = get_prime_numbers(n)

    t2 = time.time()

    print(f"time took is: {t2-t1} sec")

Quoted from Run Rust module from Python (PyO3)

To execute.

python test.py

Runs a python-based function in

python test.py --rust

Calls a Rust-based function.

result,


$ python test.py
time took is: 0.013466835021972656 sec

$ python test.py --rust
time took is: 0.0005574226379394531 sec

And the difference in execution speed became noticeable.

Summary

The ultimate goal is Being able to easily call functions and classes (like things?) Written in rust from python It was, This time I explained how to call a function written in Rust from Python. The only type conversion I've covered yet is Vec-> List, so I'll write about other types in the next article. However, the description does not change much, so it is easy.

Also, I would like to write about how to parse Rust class-like things (Struct + method) as class objects on the Python side.

This time around here.

end.

Recommended Posts

Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
[Python] How to call a c function from python (ctypes)
Call a Python function from p5.js.
Simple code to call a python program from Javascript on EC2
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Consider a conversion from a Python recursive function to a non-recursive function
[Python Kivy] How to create a simple pop up window
From setting up a Rust environment to running Hello World
Go language to see and remember Part 8 Call GO language from Python
Don't write Python if you want to speed it up with Python
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
[Road to Python Intermediate] Call a class instance like a function with __call__
Call dlm from python to run a time-varying coefficient regression model
[Python] A simple function to find the center coordinates of a circle
Everything from building a Python environment to running it on Windows
Call Matlab from Python to optimize
Numba to speed up as Python
Python-dynamically call a function from a string
How to speed up Python calculations
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
I want to pass an argument to a python function and execute it from PHP on a web server
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
Send a message from Python to Slack
Call a command from Python (Windows version)
[Python] I tried to get the type name as a string from the type function
Python program is slow! I want to speed up! In such a case ...
Send a message from Slack to a Python server
Set up a simple HTTPS server in Python 3
Edit Excel from Python to create a PivotTable
How to create a function object from a string
How to generate a Python object from JSON
Call a Python script from Embedded Python in C ++ / C ++
Set up a simple SMTP server in Python
[Go] How to write or call a function
[Python] Do your best to speed up SQLAlchemy
An easy way to call Java from Python
On a PC that cannot boot from NVMe, move / usr etc. to NVMe to speed up
Understand Python yield If you put yield in a function, it will change to a generator
[It's not too late to learn Python from 2020] Part 2 Let's create a Python development environment
A mechanism to call a Ruby method from Python that can be done in 200 lines