Skip to content

Commit

Permalink
fix(python): Release the GIL in Python APIs, part 2 of 2 (#19762)
Browse files Browse the repository at this point in the history
Co-authored-by: Itamar Turner-Trauring <[email protected]>
  • Loading branch information
itamarst and pythonspeed authored Nov 14, 2024
1 parent 1e262ba commit 869d1b9
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 173 deletions.
4 changes: 1 addition & 3 deletions crates/polars-python/src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ pub fn _execute_ir_plan_with_gpu(ir_plan_ser: Vec<u8>, py: Python) -> PyResult<P

// Execute the plan.
let mut state = ExecutionState::new();
let df = physical_plan
.execute(&mut state)
.map_err(PyPolarsErr::from)?;
let df = py.allow_threads(|| physical_plan.execute(&mut state).map_err(PyPolarsErr::from))?;

Ok(df.into())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl PyDataFrame {
}

#[staticmethod]
pub fn from_arrow_record_batches(rb: Vec<Bound<PyAny>>) -> PyResult<Self> {
let df = interop::arrow::to_rust::to_rust_df(&rb)?;
pub fn from_arrow_record_batches(py: Python, rb: Vec<Bound<PyAny>>) -> PyResult<Self> {
let df = interop::arrow::to_rust::to_rust_df(py, &rb)?;
Ok(Self::from(df))
}
}
Expand Down
28 changes: 13 additions & 15 deletions crates/polars-python/src/dataframe/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,17 @@ impl PyDataFrame {
}

#[allow(clippy::wrong_self_convention)]
pub fn to_arrow(&mut self, compat_level: PyCompatLevel) -> PyResult<Vec<PyObject>> {
self.df.align_chunks_par();
Python::with_gil(|py| {
let pyarrow = py.import_bound("pyarrow")?;
let names = self.df.get_column_names_str();
pub fn to_arrow(&mut self, py: Python, compat_level: PyCompatLevel) -> PyResult<Vec<PyObject>> {
py.allow_threads(|| self.df.align_chunks_par());
let pyarrow = py.import_bound("pyarrow")?;
let names = self.df.get_column_names_str();

let rbs = self
.df
.iter_chunks(compat_level.0, true)
.map(|rb| interop::arrow::to_py::to_py_rb(&rb, &names, py, &pyarrow))
.collect::<PyResult<_>>()?;
Ok(rbs)
})
let rbs = self
.df
.iter_chunks(compat_level.0, true)
.map(|rb| interop::arrow::to_py::to_py_rb(&rb, &names, py, &pyarrow))
.collect::<PyResult<_>>()?;
Ok(rbs)
}

/// Create a `Vec` of PyArrow RecordBatch instances.
Expand All @@ -100,8 +98,8 @@ impl PyDataFrame {
/// since those can't be converted correctly via PyArrow. The calling Python
/// code should make sure these are not included.
#[allow(clippy::wrong_self_convention)]
pub fn to_pandas(&mut self) -> PyResult<Vec<PyObject>> {
self.df.as_single_chunk_par();
pub fn to_pandas(&mut self, py: Python) -> PyResult<Vec<PyObject>> {
py.allow_threads(|| self.df.as_single_chunk_par());
Python::with_gil(|py| {
let pyarrow = py.import_bound("pyarrow")?;
let names = self.df.get_column_names_str();
Expand Down Expand Up @@ -154,7 +152,7 @@ impl PyDataFrame {
py: Python<'py>,
requested_schema: Option<PyObject>,
) -> PyResult<Bound<'py, PyCapsule>> {
self.df.align_chunks_par();
py.allow_threads(|| self.df.align_chunks_par());
dataframe_to_stream(&self.df, py)
}
}
Loading

0 comments on commit 869d1b9

Please sign in to comment.