From b5157b043113bca72b37ad9b9e7e40dd0153835c Mon Sep 17 00:00:00 2001 From: kngwyu Date: Mon, 28 Oct 2019 13:21:30 +0900 Subject: [PATCH] Add doctests for allow_threads --- src/python.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/python.rs b/src/python.rs index f9a0362a46c..f9164f2519b 100644 --- a/src/python.rs +++ b/src/python.rs @@ -72,6 +72,53 @@ impl<'p> Python<'p> { } /// Temporarily releases the `GIL`, thus allowing other Python threads to run. + /// + /// # Example + /// ``` + /// # use pyo3::prelude::*; use pyo3::types::IntoPyDict; use pyo3::wrap_pyfunction; + /// use pyo3::exceptions::RuntimeError; + /// use std::sync::Arc; + /// use std::thread; + /// #[pyfunction] + /// fn parallel_count(py: Python<'_>, strings: Vec, query: String) -> PyResult { + /// let query = query.chars().next().unwrap(); + /// py.allow_threads(move || { + /// let threads: Vec<_> = strings + /// .into_iter() + /// .map(|s| thread::spawn(move || s.chars().filter(|&c| c == query).count())) + /// .collect(); + /// let mut sum = 0; + /// for t in threads { + /// sum += t.join().map_err(|_| PyErr::new::(()))?; + /// } + /// Ok(sum) + /// }) + /// } + /// let gil = Python::acquire_gil(); + /// let py = gil.python(); + /// let m = PyModule::new(py, "pcount").unwrap(); + /// m.add_wrapped(wrap_pyfunction!(parallel_count)).unwrap(); + /// let locals = [("pcount", m)].into_py_dict(py); + /// py.run(r#" + /// s = ["Flow", "my", "tears", "the", "Policeman", "Said"] + /// assert pcount.parallel_count(s, "a") == 3 + /// "#, None, Some(locals)); + /// ``` + /// + /// **NOTE** + /// You cannot use all `&Py~` types in the closure that `allow_threads` takes. + /// # Example + /// ```compile_fail + /// # use pyo3::prelude::*; + /// # use pyo3::types::PyString; + /// fn parallel_print(py: Python<'_>) { + /// let s = PyString::new(py, "This object should not be shared >_<"); + /// py.allow_threads(move || { + /// println!("{:?}", s); // This causes compile error. + /// }); + /// } + /// # Example + /// ``` pub fn allow_threads(self, f: F) -> T where F: Send + FnOnce() -> T,