1- use pyo3:: { IntoPyObjectExt , prelude:: * } ;
2- use std:: {
3- future:: Future ,
4- sync:: Arc ,
5- } ;
6- use tokio:: {
7- runtime:: Builder as RuntimeBuilder ,
8- task:: JoinHandle ,
9- } ;
101use futures:: FutureExt ;
2+ use pyo3:: { prelude:: * , IntoPyObjectExt } ;
3+ use std:: { future:: Future , sync:: Arc } ;
4+ use tokio:: { runtime:: Builder as RuntimeBuilder , task:: JoinHandle } ;
115
126#[ cfg( unix) ]
137use super :: callbacks:: PyFutureAwaitable ;
@@ -74,7 +68,11 @@ impl RuntimeWrapper {
7468 }
7569
7670 pub fn handler ( & self ) -> RuntimeRef {
77- RuntimeRef :: new ( self . inner . handle ( ) . clone ( ) , self . br . clone ( ) , self . pr . clone ( ) )
71+ RuntimeRef :: new (
72+ self . inner . handle ( ) . clone ( ) ,
73+ self . br . clone ( ) ,
74+ self . pr . clone ( ) ,
75+ )
7876 }
7977}
8078
@@ -86,7 +84,11 @@ pub struct RuntimeRef {
8684}
8785
8886impl RuntimeRef {
89- pub fn new ( rt : tokio:: runtime:: Handle , br : Arc < BlockingRunner > , pyloop : Arc < Py < PyAny > > ) -> Self {
87+ pub fn new (
88+ rt : tokio:: runtime:: Handle ,
89+ br : Arc < BlockingRunner > ,
90+ pyloop : Arc < Py < PyAny > > ,
91+ ) -> Self {
9092 Self {
9193 inner : rt,
9294 innerb : br,
@@ -141,7 +143,10 @@ pub(crate) fn empty_future_into_py(py: Python) -> PyResult<Bound<PyAny>> {
141143}
142144
143145#[ inline( always) ]
144- pub ( crate ) fn done_future_into_py ( py : Python , result : PyResult < Py < PyAny > > ) -> PyResult < Bound < PyAny > > {
146+ pub ( crate ) fn done_future_into_py (
147+ py : Python ,
148+ result : PyResult < Py < PyAny > > ,
149+ ) -> PyResult < Bound < PyAny > > {
145150 PyDoneAwaitable :: new ( result) . into_bound_py_any ( py)
146151}
147152
@@ -256,23 +261,28 @@ where
256261 // We have a handle, use our optimized implementation
257262 // Get the event loop from Python
258263 let asyncio = py. import ( "asyncio" ) ?;
259- let event_loop = asyncio. call_method0 ( "get_event_loop" )
264+ let event_loop = asyncio
265+ . call_method0 ( "get_event_loop" )
260266 . or_else ( |_| asyncio. call_method0 ( "new_event_loop" ) ) ?;
261267 let event_loop: Py < PyAny > = event_loop. unbind ( ) ;
262268
263269 // Create a simple blocking runner (1 thread, 30s timeout)
264270 let blocking_runner = Arc :: new ( BlockingRunner :: new ( 1 , 30 ) ) ;
265-
271+
266272 // Create RuntimeRef
267273 let rt_ref = RuntimeRef :: new ( rt_handle, blocking_runner, Arc :: new ( event_loop) ) ;
268274
269275 // Convert the future to use FutureResultToPy
270276 let wrapped_fut = async move {
271277 match fut. await {
272278 Ok ( ( ) ) => FutureResultToPy :: None ,
273- Err ( e) => FutureResultToPy :: Err ( Err ( PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > (
274- format ! ( "Future error: {}" , e)
275- ) ) ) ,
279+ Err ( e) => FutureResultToPy :: Err ( Err ( PyErr :: new :: <
280+ pyo3:: exceptions:: PyRuntimeError ,
281+ _ ,
282+ > ( format ! (
283+ "Future error: {}" ,
284+ e
285+ ) ) ) ) ,
276286 }
277287 } ;
278288
@@ -284,12 +294,14 @@ where
284294 // This happens when called from Python code that's not in a tokio async context
285295 // Convert Result<(), anyhow::Error> to PyResult<()> for pyo3_async_runtimes
286296 let py_fut = fut. map ( |result| {
287- result. map_err ( |e| PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > (
288- format ! ( "Future error: {}" , e)
289- ) )
297+ result. map_err ( |e| {
298+ PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > ( format ! (
299+ "Future error: {}" ,
300+ e
301+ ) )
302+ } )
290303 } ) ;
291304 pyo3_async_runtimes:: tokio:: future_into_py ( py, py_fut)
292305 }
293306 }
294307}
295-
0 commit comments