Parallel run #[pyfunction]
to get multi PyObject
in Rust
#3027
-
#[pyfunction]
pub fn get_dict(py: Python) -> PyObject {
let map = PyDict::new(py);
SYMBOL_MAP.iter().for_each(|(k, v)| {
map.set_item(v.name.clone(), k.to_string()).expect("Failed to set_item on dict");
});
map.into()
} If I call this func as a python extension function it would trigger gil definitely which makes multi thread not efficiency. I want to create multi Dict in rust and give the dict to python after, if I just call this func in pure Rust env with rayon, would it be real parallelism? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can't interact with Python objects from multiple threads, so you'll need to use a thread-safe datastructure to build the map. Perhaps |
Beta Was this translation helpful? Give feedback.
You can't interact with Python objects from multiple threads, so you'll need to use a thread-safe datastructure to build the map. Perhaps
hashbrown::HashMap
with thehashbrown/rayon
feature enabled. You can then usepyo3/hashbrown
feature to enable conversion ofhash brown::HashMap
toPyDict
once the multithreaded part is complete.