@@ -43,17 +43,16 @@ use pyo3::prelude::*;
4343use pyo3:: pybacked:: PyBackedStr ;
4444use pyo3:: types:: { PyCapsule , PyList , PyTuple , PyTupleMethods } ;
4545use rayon:: prelude:: * ;
46- use tokio:: time:: { sleep, Duration } ;
4746
4847use crate :: catalog:: PyTable ;
49- use crate :: errors:: { py_datafusion_err, to_datafusion_err , PyDataFusionError } ;
48+ use crate :: errors:: { py_datafusion_err, PyDataFusionError } ;
5049use crate :: expr:: sort_expr:: to_sort_expressions;
5150use crate :: physical_plan:: PyExecutionPlan ;
5251use crate :: record_batch:: PyRecordBatchStream ;
5352use crate :: sql:: logical:: PyLogicalPlan ;
5453use crate :: utils:: {
5554 get_tokio_runtime, is_ipython_env, py_obj_to_scalar_value, spawn_and_wait, validate_pycapsule,
56- wait_for_future,
55+ wait_for_future, wait_for_stream_next ,
5756} ;
5857use crate :: {
5958 errors:: PyDataFusionResult ,
@@ -1018,30 +1017,10 @@ impl Iterator for ArrowStreamReader {
10181017 type Item = Result < RecordBatch , ArrowError > ;
10191018
10201019 fn next ( & mut self ) -> Option < Self :: Item > {
1021- const INTERVAL_CHECK_SIGNALS : Duration = Duration :: from_millis ( 1_000 ) ;
1022- let rt = & get_tokio_runtime ( ) . 0 ;
1023- let fut = self . stream . next ( ) ;
1024-
1025- let result = Python :: with_gil ( |py| {
1026- py. allow_threads ( || {
1027- rt. block_on ( async {
1028- tokio:: pin!( fut) ;
1029- loop {
1030- tokio:: select! {
1031- res = & mut fut => break res,
1032- _ = sleep( INTERVAL_CHECK_SIGNALS ) => {
1033- if let Err ( err) = Python :: with_gil( |py| py. check_signals( ) ) {
1034- break Some ( Err ( to_datafusion_err( err) ) ) ;
1035- }
1036- }
1037- }
1038- }
1039- } )
1040- } )
1041- } ) ;
1020+ let result = Python :: with_gil ( |py| wait_for_stream_next ( py, & mut self . stream ) ) ;
10421021
10431022 match result {
1044- Some ( Ok ( batch) ) => {
1023+ Ok ( Some ( batch) ) => {
10451024 let batch = if self . project {
10461025 match record_batch_into_schema ( batch, self . schema . as_ref ( ) ) {
10471026 Ok ( b) => b,
@@ -1052,8 +1031,8 @@ impl Iterator for ArrowStreamReader {
10521031 } ;
10531032 Some ( Ok ( batch) )
10541033 }
1055- Some ( Err ( e ) ) => Some ( Err ( ArrowError :: from ( e ) ) ) ,
1056- None => None ,
1034+ Ok ( None ) => None ,
1035+ Err ( e ) => Some ( Err ( ArrowError :: from ( e ) ) ) ,
10571036 }
10581037 }
10591038}
0 commit comments