Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release the Python interpreter lock while waiting for data #39

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions runtime/src/operator/python.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{OperatorEvent, OperatorInput};
use eyre::{bail, eyre, Context};
use pyo3::{pyclass, types::IntoPyDict, Python};
use pyo3::{pyclass, types::IntoPyDict, Py, Python};
use std::{
panic::{catch_unwind, AssertUnwindSafe},
path::Path,
Expand All @@ -25,7 +25,7 @@ pub fn spawn(
events_tx: events_tx.clone(),
};

let python_runner = move |py: pyo3::Python| {
let init_operator = move |py: Python| {
if let Some(parent_path) = path.parent() {
let parent_path = parent_path
.to_str()
Expand Down Expand Up @@ -58,32 +58,42 @@ pub fn spawn(
let operator = py
.eval("Operator()", None, Some(locals))
.wrap_err("failed to create Operator instance")?;
Result::<_, eyre::Report>::Ok(Py::from(operator))
};

let python_runner = move || {
let operator =
Python::with_gil(init_operator).wrap_err("failed to init python operator")?;

while let Some(input) = inputs.blocking_recv() {
operator
.call_method1(
Python::with_gil(|py| {
operator.call_method1(
py,
"on_input",
(input.id.to_string(), input.value, send_output.clone()),
)
.wrap_err("on_input failed")?;
})
.wrap_err("on_input failed")?;
}

if operator
.hasattr("drop_operator")
.wrap_err("failed to look for drop_operator")?
{
operator
.call_method0("drop_operator")
.wrap_err("drop_operator failed")?;
}
Python::with_gil(|py| {
let operator = operator.as_ref(py);
if operator
.hasattr("drop_operator")
.wrap_err("failed to look for drop_operator")?
{
operator.call_method0("drop_operator")?;
}
Result::<_, eyre::Report>::Ok(())
})?;

Result::<_, eyre::Report>::Ok(())
};

thread::spawn(move || {
let closure = AssertUnwindSafe(|| {
let result = Python::with_gil(python_runner);
result.wrap_err_with(|| format!("error in Python module at {}", path_cloned.display()))
python_runner()
.wrap_err_with(|| format!("error in Python module at {}", path_cloned.display()))
});

match catch_unwind(closure) {
Expand Down