Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hokim98/pyo3-mp
https://github.com/hokim98/pyo3-mp
Last synced: about 8 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/hokim98/pyo3-mp
- Owner: HoKim98
- License: other
- Created: 2020-09-24T16:47:59.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-02T05:11:18.000Z (almost 4 years ago)
- Last Synced: 2024-08-10T23:23:42.519Z (3 months ago)
- Language: Rust
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PyO3-MultiProcessing
Rust bindings for Python multiprocessing module.
## Installation
``` toml
[dependencies]
pyo3-mp = "*"
```## Usage
``` 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.
#[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> {
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()
})
}
```