pyo3-mp

Crates.iopyo3-mp
lib.rspyo3-mp
version0.1.3
sourcesrc
created_at2020-09-24 16:50:06.174963
updated_at2020-12-02 05:11:49.746712
descriptionRust bindings for Python multiprocessing module
homepagehttps://github.com/kerryeon/pyo3-mp
repositoryhttps://github.com/kerryeon/pyo3-mp
max_upload_size
id292580
size21,316
Ho Kim (kerryeon)

documentation

https://docs.rs/crate/pyo3-mp/

README

PyO3-MultiProcessing

Rust bindings for Python multiprocessing module.

Installation

[dependencies]
pyo3-mp = "*"

Usage

use std::thread::sleep;
use std::time::Duration;

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

use pyo3_mp::Process;

/// A Python function implemented in Rust.
#[pyfunction]
fn foo(_py: Python, i: usize) -> PyResult<()> {
    println!("hello, number {}!", i);
    // This may be worked on each process!
    sleep(Duration::from_secs(1));
    println!("goodbye, number {}!", i);
    Ok(())
}

/// Converts the pyfunction into python object.
fn build_foo<'a>(py: Python<'a>) -> PyResult<Py<PyAny>> {
    Ok(wrap_pyfunction!(foo)(py)?.into_py(py))
}

fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        // Let's get a sample python function.
        let foo = build_foo(py)?;

        let mut mp = Process::new(py)?;

        // Spawn 10 processes.
        for i in 0..10 {
            mp.spawn(&foo, (i,), None)?;
        }

        mp.join()
    })
}
Commit count: 6

cargo fmt