Skip to content

Commit 63892c1

Browse files
ngoldbaumzsol
authored andcommitted
call py.allow_threads before expensive calls in python bindings
1 parent c224665 commit 63892c1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

native/libcst/src/py.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,30 @@ use pyo3::prelude::*;
88

99
#[pymodule(gil_used = false)]
1010
#[pyo3(name = "native")]
11-
pub fn libcst_native(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
11+
pub fn libcst_native(m: &Bound<PyModule>) -> PyResult<()> {
1212
#[pyfn(m)]
1313
#[pyo3(signature = (source, encoding=None))]
1414
fn parse_module(source: String, encoding: Option<&str>) -> PyResult<PyObject> {
15-
let m = crate::parse_module(source.as_str(), encoding)?;
16-
Python::with_gil(|py| m.try_into_py(py))
15+
Python::with_gil(|py| {
16+
let m = py.allow_threads(|| crate::parse_module(source.as_str(), encoding))?;
17+
m.try_into_py(py)
18+
})
1719
}
1820

1921
#[pyfn(m)]
2022
fn parse_expression(source: String) -> PyResult<PyObject> {
21-
let expr = crate::parse_expression(source.as_str())?;
22-
Python::with_gil(|py| expr.try_into_py(py))
23+
Python::with_gil(|py| {
24+
let expr = py.allow_threads(|| crate::parse_expression(source.as_str()))?;
25+
expr.try_into_py(py)
26+
})
2327
}
2428

2529
#[pyfn(m)]
2630
fn parse_statement(source: String) -> PyResult<PyObject> {
27-
let stm = crate::parse_statement(source.as_str())?;
28-
Python::with_gil(|py| stm.try_into_py(py))
31+
Python::with_gil(|py| {
32+
let stm = py.allow_threads(|| crate::parse_statement(source.as_str()))?;
33+
stm.try_into_py(py)
34+
})
2935
}
3036

3137
Ok(())

0 commit comments

Comments
 (0)