Skip to content

Commit

Permalink
Release GIL when executing transformer chain
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Oct 7, 2024
1 parent 1add671 commit c6dbe6d
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/api/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,11 @@ impl PythonTransformer {
/// --------
/// >>> x = Expression.symbol('x')
/// >>> e = Transformer().expand()((1+x)**2)
pub fn __call__(&self, expr: ConvertibleToExpression) -> PyResult<PythonExpression> {
pub fn __call__(
&self,
expr: ConvertibleToExpression,
py: Python,
) -> PyResult<PythonExpression> {
let e = expr.to_expression();

if let Pattern::Transformer(t) = &self.expr {
Expand All @@ -409,14 +413,16 @@ impl PythonTransformer {

let mut out = Atom::new();

Workspace::get_local()
.with(|ws| Transformer::execute_chain(e.as_view(), &t.1, ws, &mut out))
.map_err(|e| match e {
TransformerError::Interrupt => {
exceptions::PyKeyboardInterrupt::new_err("Interrupted by user")
}
TransformerError::ValueError(v) => exceptions::PyValueError::new_err(v),
})?;
py.allow_threads(|| {
Workspace::get_local()
.with(|ws| Transformer::execute_chain(e.as_view(), &t.1, ws, &mut out))
.map_err(|e| match e {
TransformerError::Interrupt => {
exceptions::PyKeyboardInterrupt::new_err("Interrupted by user")
}
TransformerError::ValueError(v) => exceptions::PyValueError::new_err(v),
})
})?;

Ok(out.into())
} else {
Expand Down Expand Up @@ -922,22 +928,25 @@ impl PythonTransformer {
/// >>> e = (x+1)**5
/// >>> e = e.transform().expand().execute()
/// >>> print(e)
pub fn execute(&self) -> PyResult<PythonExpression> {
pub fn execute(&self, py: Python) -> PyResult<PythonExpression> {
let mut out = Atom::default();
Workspace::get_local()
.with(|workspace| {
self.expr.substitute_wildcards(
workspace,
&mut out,
&MatchStack::new(&Condition::default(), &MatchSettings::default()),
)
})
.map_err(|e| match e {
TransformerError::Interrupt => {
exceptions::PyKeyboardInterrupt::new_err("Interrupted by user")
}
TransformerError::ValueError(v) => exceptions::PyValueError::new_err(v),
})?;

py.allow_threads(|| {
Workspace::get_local()
.with(|workspace| {
self.expr.substitute_wildcards(
workspace,
&mut out,
&MatchStack::new(&Condition::default(), &MatchSettings::default()),
)
})
.map_err(|e| match e {
TransformerError::Interrupt => {
exceptions::PyKeyboardInterrupt::new_err("Interrupted by user")
}
TransformerError::ValueError(v) => exceptions::PyValueError::new_err(v),
})
})?;

Ok(out.into())
}
Expand Down

0 comments on commit c6dbe6d

Please sign in to comment.