From 63892c154ca4c8fd4a6d7fc56b5f96bb47acb1ea Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Thu, 10 Apr 2025 16:31:42 -0600 Subject: [PATCH] call py.allow_threads before expensive calls in python bindings --- native/libcst/src/py.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/native/libcst/src/py.rs b/native/libcst/src/py.rs index 57da11e78..5b3eaa03f 100644 --- a/native/libcst/src/py.rs +++ b/native/libcst/src/py.rs @@ -8,24 +8,30 @@ use pyo3::prelude::*; #[pymodule(gil_used = false)] #[pyo3(name = "native")] -pub fn libcst_native(_py: Python, m: &Bound) -> PyResult<()> { +pub fn libcst_native(m: &Bound) -> PyResult<()> { #[pyfn(m)] #[pyo3(signature = (source, encoding=None))] fn parse_module(source: String, encoding: Option<&str>) -> PyResult { - let m = crate::parse_module(source.as_str(), encoding)?; - Python::with_gil(|py| m.try_into_py(py)) + Python::with_gil(|py| { + let m = py.allow_threads(|| crate::parse_module(source.as_str(), encoding))?; + m.try_into_py(py) + }) } #[pyfn(m)] fn parse_expression(source: String) -> PyResult { - let expr = crate::parse_expression(source.as_str())?; - Python::with_gil(|py| expr.try_into_py(py)) + Python::with_gil(|py| { + let expr = py.allow_threads(|| crate::parse_expression(source.as_str()))?; + expr.try_into_py(py) + }) } #[pyfn(m)] fn parse_statement(source: String) -> PyResult { - let stm = crate::parse_statement(source.as_str())?; - Python::with_gil(|py| stm.try_into_py(py)) + Python::with_gil(|py| { + let stm = py.allow_threads(|| crate::parse_statement(source.as_str()))?; + stm.try_into_py(py) + }) } Ok(())