@@ -288,6 +288,40 @@ where
288
288
generic:: future_into_py_with_loop :: < AsyncStdRuntime , F > ( event_loop, fut)
289
289
}
290
290
291
+ /// Convert a Rust Future into a Python awaitable
292
+ ///
293
+ /// # Arguments
294
+ /// * `event_loop` - The Python event loop that the awaitable should be attached to
295
+ /// * `fut` - The Rust future to be converted
296
+ ///
297
+ /// # Examples
298
+ ///
299
+ /// ```
300
+ /// use std::time::Duration;
301
+ ///
302
+ /// use pyo3::prelude::*;
303
+ ///
304
+ /// /// Awaitable sleep function
305
+ /// #[pyfunction]
306
+ /// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
307
+ /// let secs = secs.extract()?;
308
+ /// pyo3_asyncio::async_std::future_into_py_with_locals(
309
+ /// py,
310
+ /// pyo3_asyncio::async_std::get_current_locals(py)?,
311
+ /// async move {
312
+ /// async_std::task::sleep(Duration::from_secs(secs)).await;
313
+ /// Python::with_gil(|py| Ok(py.None()))
314
+ /// }
315
+ /// )
316
+ /// }
317
+ /// ```
318
+ pub fn future_into_py_with_locals < F > ( py : Python , locals : TaskLocals , fut : F ) -> PyResult < & PyAny >
319
+ where
320
+ F : Future < Output = PyResult < PyObject > > + Send + ' static ,
321
+ {
322
+ generic:: future_into_py_with_locals :: < AsyncStdRuntime , F > ( py, locals, fut)
323
+ }
324
+
291
325
/// Convert a Rust Future into a Python awaitable
292
326
///
293
327
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
@@ -458,6 +492,59 @@ where
458
492
generic:: local_future_into_py_with_loop :: < AsyncStdRuntime , _ > ( event_loop, fut)
459
493
}
460
494
495
+ /// Convert a `!Send` Rust Future into a Python awaitable
496
+ ///
497
+ /// # Arguments
498
+ /// * `event_loop` - The Python event loop that the awaitable should be attached to
499
+ /// * `fut` - The Rust future to be converted
500
+ ///
501
+ /// # Examples
502
+ ///
503
+ /// ```
504
+ /// use std::{rc::Rc, time::Duration};
505
+ ///
506
+ /// use pyo3::prelude::*;
507
+ ///
508
+ /// /// Awaitable non-send sleep function
509
+ /// #[pyfunction]
510
+ /// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
511
+ /// // Rc is non-send so it cannot be passed into pyo3_asyncio::async_std::future_into_py
512
+ /// let secs = Rc::new(secs);
513
+ /// Ok(pyo3_asyncio::async_std::local_future_into_py_with_locals(
514
+ /// py,
515
+ /// pyo3_asyncio::async_std::get_current_locals(py)?,
516
+ /// async move {
517
+ /// async_std::task::sleep(Duration::from_secs(*secs)).await;
518
+ /// Python::with_gil(|py| Ok(py.None()))
519
+ /// }
520
+ /// )?.into())
521
+ /// }
522
+ ///
523
+ /// # #[cfg(all(feature = "async-std-runtime", feature = "attributes"))]
524
+ /// #[pyo3_asyncio::async_std::main]
525
+ /// async fn main() -> PyResult<()> {
526
+ /// Python::with_gil(|py| {
527
+ /// let py_future = sleep_for(py, 1)?;
528
+ /// pyo3_asyncio::async_std::into_future(py_future)
529
+ /// })?
530
+ /// .await?;
531
+ ///
532
+ /// Ok(())
533
+ /// }
534
+ /// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
535
+ /// # fn main() {}
536
+ /// ```
537
+ pub fn local_future_into_py_with_locals < F > (
538
+ py : Python ,
539
+ locals : TaskLocals ,
540
+ fut : F ,
541
+ ) -> PyResult < & PyAny >
542
+ where
543
+ F : Future < Output = PyResult < PyObject > > + ' static ,
544
+ {
545
+ generic:: local_future_into_py_with_locals :: < AsyncStdRuntime , _ > ( py, locals, fut)
546
+ }
547
+
461
548
/// Convert a `!Send` Rust Future into a Python awaitable
462
549
///
463
550
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running when
0 commit comments