Rust bindings for Python multiprocessing module.
toml
[dependencies]
pyo3-mp = "*"
``` rust 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.
fn foo(py: Python, i: usize) -> PyResult<()> { println!("hello, number {}!", i); // This may be worked on each process! sleep(Duration::fromsecs(1)); println!("goodbye, number {}!", i); Ok(()) }
/// Converts the pyfunction into python object.
fn buildfoo<'a>(py: Python<'a>) -> PyResult
fn main() -> PyResult<()> { Python::withgil(|py| { // Let's get a sample python function. let foo = buildfoo(py)?;
let mut mp = Process::new(py)?;
// Spawn 10 processes.
for i in 0..10 {
mp.spawn(&foo, (i,), None)?;
}
mp.join()
})
} ```