Rust call multiple python processes #2876
-
I have succeed that create a python extension with Pyo3, and this could send a ptr of apache arrow data form rust to python. Assume I have a main rust thread which has some big apache arrow data, and I want to share it to several Python interpreter for calculation. Can I use multi processes(GIL not work for thread) in rust to initialize multi independent Python interpreter and share the data ptr? Like a rust call multiple python fn thread_fn1() {
pyo3::prepare_freethreaded_python();
for _ in 0..10 {
Python::with_gil(|py| -> PyResult<()> {
//get some data ref from rust main thread
}).unwrap();
}
}
fn thread_fn2() {
pyo3::prepare_freethreaded_python();
for _ in 0..10 {
Python::with_gil(|py| -> PyResult<()> {
//get some data ref from rust main thread
}).unwrap();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
If these are single-shot computations, you could fork the main process after receiving the data and do the computations in the child processes. (Just spawning threads is not sufficient as Python will use process-wide data structures and locks like the GIL, so the work would done by |
Beta Was this translation helpful? Give feedback.
-
Note that rust and forking don't really work well (there is nothing in std to do forking, for example). You can't do multiprocessing with pyo3. |
Beta Was this translation helpful? Give feedback.
If these are single-shot computations, you could fork the main process after receiving the data and do the computations in the child processes. (Just spawning threads is not sufficient as Python will use process-wide data structures and locks like the GIL, so the work would done by
thread_fn1
andthread_fn2
would run serially.)