@@ -22,7 +22,6 @@ use crate::utils::{wait_for_future, wait_for_stream_next};
2222use datafusion:: arrow:: pyarrow:: ToPyArrow ;
2323use datafusion:: arrow:: record_batch:: RecordBatch ;
2424use datafusion:: physical_plan:: SendableRecordBatchStream ;
25- use futures:: StreamExt ;
2625use pyo3:: exceptions:: { PyStopAsyncIteration , PyStopIteration } ;
2726use pyo3:: prelude:: * ;
2827use pyo3:: { pyclass, pymethods, PyObject , PyResult , Python } ;
@@ -91,10 +90,15 @@ async fn next_stream(
9190 stream : Arc < Mutex < SendableRecordBatchStream > > ,
9291 sync : bool ,
9392) -> PyResult < PyRecordBatch > {
94- let mut stream = stream. lock ( ) . await ;
95- match stream. next ( ) . await {
96- Some ( Ok ( batch) ) => Ok ( batch. into ( ) ) ,
97- Some ( Err ( e) ) => Err ( PyDataFusionError :: from ( e) ) ?,
93+ let result = tokio:: task:: spawn_blocking ( move || {
94+ let mut stream = stream. blocking_lock ( ) ;
95+ Python :: with_gil ( |py| wait_for_stream_next ( py, & mut stream) )
96+ } )
97+ . await
98+ . map_err ( |e| PyDataFusionError :: Common ( e. to_string ( ) ) ) ?;
99+
100+ match result. map_err ( PyDataFusionError :: from) ? {
101+ Some ( batch) => Ok ( batch. into ( ) ) ,
98102 None => {
99103 // Depending on whether the iteration is sync or not, we raise either a
100104 // StopIteration or a StopAsyncIteration
0 commit comments