@@ -590,6 +590,10 @@ impl PyDoneCallback {
590
590
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
591
591
/// the `asyncio.Future` is cancelled from Python.
592
592
///
593
+ /// __This function will be deprecated in favor of [`future_into_py_with_loop`] in `v0.15` because
594
+ /// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
595
+ /// replaced with [`future_into_py_with_loop`].__
596
+ ///
593
597
/// # Arguments
594
598
/// * `event_loop` - The Python event loop that the awaitable should be attached to
595
599
/// * `fut` - The Rust future to be converted
@@ -776,6 +780,10 @@ where
776
780
/// Unlike [`future_into_py`], this function will stop the Rust future from running when the
777
781
/// `asyncio.Future` is cancelled from Python.
778
782
///
783
+ /// __This function will be deprecated in favor of [`future_into_py`] in `v0.15` because
784
+ /// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
785
+ /// replaced with [`future_into_py`].__
786
+ ///
779
787
/// # Arguments
780
788
/// * `py` - The current PyO3 GIL guard
781
789
/// * `fut` - The Rust future to be converted
@@ -1097,9 +1105,13 @@ where
1097
1105
1098
1106
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
1099
1107
///
1100
- /// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running
1108
+ /// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running
1101
1109
/// when the `asyncio.Future` is cancelled from Python.
1102
1110
///
1111
+ /// __This function will be deprecated in favor of [`local_future_into_py_with_loop`] in `v0.15` because
1112
+ /// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1113
+ /// replaced with [`local_future_into_py_with_loop`].__
1114
+ ///
1103
1115
/// # Arguments
1104
1116
/// * `event_loop` - The Python event loop that the awaitable should be attached to
1105
1117
/// * `fut` - The Rust future to be converted
@@ -1109,7 +1121,7 @@ where
1109
1121
/// ```no_run
1110
1122
/// # use std::{task::{Context, Poll}, pin::Pin, future::Future};
1111
1123
/// #
1112
- /// # use pyo3_asyncio::generic::{JoinError, Runtime};
1124
+ /// # use pyo3_asyncio::generic::{JoinError, SpawnLocalExt, Runtime};
1113
1125
/// #
1114
1126
/// # struct MyCustomJoinError;
1115
1127
/// #
@@ -1159,13 +1171,29 @@ where
1159
1171
/// # }
1160
1172
/// # }
1161
1173
/// #
1162
- /// use std::time::Duration;
1174
+ /// # impl SpawnLocalExt for MyCustomRuntime {
1175
+ /// # fn scope_local<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R>>>
1176
+ /// # where
1177
+ /// # F: Future<Output = R> + 'static
1178
+ /// # {
1179
+ /// # unreachable!()
1180
+ /// # }
1181
+ /// #
1182
+ /// # fn spawn_local<F>(fut: F) -> Self::JoinHandle
1183
+ /// # where
1184
+ /// # F: Future<Output = ()> + 'static
1185
+ /// # {
1186
+ /// # unreachable!()
1187
+ /// # }
1188
+ /// # }
1189
+ /// #
1190
+ /// use std::{rc::Rc, time::Duration};
1163
1191
///
1164
1192
/// use pyo3::prelude::*;
1165
1193
///
1166
1194
/// /// Awaitable sleep function
1167
1195
/// #[pyfunction]
1168
- /// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny ) -> PyResult<&'p PyAny> {
1196
+ /// fn sleep_for<'p>(py: Python<'p>, secs: u64 ) -> PyResult<&'p PyAny> {
1169
1197
/// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
1170
1198
/// let secs = Rc::new(secs);
1171
1199
///
@@ -1178,7 +1206,10 @@ where
1178
1206
/// )
1179
1207
/// }
1180
1208
/// ```
1181
- pub fn local_cancellable_future_into_py_with_loop < R , F > ( event_loop : & PyAny , fut : F ) -> PyResult < & PyAny >
1209
+ pub fn local_cancellable_future_into_py_with_loop < R , F > (
1210
+ event_loop : & PyAny ,
1211
+ fut : F ,
1212
+ ) -> PyResult < & PyAny >
1182
1213
where
1183
1214
R : Runtime + SpawnLocalExt ,
1184
1215
F : Future < Output = PyResult < PyObject > > + ' static ,
@@ -1300,3 +1331,113 @@ where
1300
1331
{
1301
1332
local_future_into_py_with_loop :: < R , F > ( get_current_loop :: < R > ( py) ?, fut)
1302
1333
}
1334
+ /// Convert a Rust Future into a Python awaitable with a generic runtime
1335
+ ///
1336
+ /// Unlike [`future_into_py`], this function will stop the Rust future from running when the
1337
+ /// `asyncio.Future` is cancelled from Python.
1338
+ ///
1339
+ /// __This function will be deprecated in favor of [`local_future_into_py`] in `v0.15` because
1340
+ /// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1341
+ /// replaced with [`local_future_into_py`].__
1342
+ ///
1343
+ /// # Arguments
1344
+ /// * `py` - The current PyO3 GIL guard
1345
+ /// * `fut` - The Rust future to be converted
1346
+ ///
1347
+ /// # Examples
1348
+ ///
1349
+ /// ```no_run
1350
+ /// # use std::{task::{Context, Poll}, pin::Pin, future::Future};
1351
+ /// #
1352
+ /// # use pyo3_asyncio::generic::{JoinError, SpawnLocalExt, Runtime};
1353
+ /// #
1354
+ /// # struct MyCustomJoinError;
1355
+ /// #
1356
+ /// # impl JoinError for MyCustomJoinError {
1357
+ /// # fn is_panic(&self) -> bool {
1358
+ /// # unreachable!()
1359
+ /// # }
1360
+ /// # }
1361
+ /// #
1362
+ /// # struct MyCustomJoinHandle;
1363
+ /// #
1364
+ /// # impl Future for MyCustomJoinHandle {
1365
+ /// # type Output = Result<(), MyCustomJoinError>;
1366
+ /// #
1367
+ /// # fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
1368
+ /// # unreachable!()
1369
+ /// # }
1370
+ /// # }
1371
+ /// #
1372
+ /// # struct MyCustomRuntime;
1373
+ /// #
1374
+ /// # impl MyCustomRuntime {
1375
+ /// # async fn sleep(_: Duration) {
1376
+ /// # unreachable!()
1377
+ /// # }
1378
+ /// # }
1379
+ /// #
1380
+ /// # impl Runtime for MyCustomRuntime {
1381
+ /// # type JoinError = MyCustomJoinError;
1382
+ /// # type JoinHandle = MyCustomJoinHandle;
1383
+ /// #
1384
+ /// # fn scope<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R> + Send>>
1385
+ /// # where
1386
+ /// # F: Future<Output = R> + Send + 'static
1387
+ /// # {
1388
+ /// # unreachable!()
1389
+ /// # }
1390
+ /// # fn get_task_event_loop(py: Python) -> Option<&PyAny> {
1391
+ /// # unreachable!()
1392
+ /// # }
1393
+ /// #
1394
+ /// # fn spawn<F>(fut: F) -> Self::JoinHandle
1395
+ /// # where
1396
+ /// # F: Future<Output = ()> + Send + 'static
1397
+ /// # {
1398
+ /// # unreachable!()
1399
+ /// # }
1400
+ /// # }
1401
+ /// #
1402
+ /// # impl SpawnLocalExt for MyCustomRuntime {
1403
+ /// # fn scope_local<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R>>>
1404
+ /// # where
1405
+ /// # F: Future<Output = R> + 'static
1406
+ /// # {
1407
+ /// # unreachable!()
1408
+ /// # }
1409
+ /// #
1410
+ /// # fn spawn_local<F>(fut: F) -> Self::JoinHandle
1411
+ /// # where
1412
+ /// # F: Future<Output = ()> + 'static
1413
+ /// # {
1414
+ /// # unreachable!()
1415
+ /// # }
1416
+ /// # }
1417
+ /// #
1418
+ /// use std::{rc::Rc, time::Duration};
1419
+ ///
1420
+ /// use pyo3::prelude::*;
1421
+ ///
1422
+ /// /// Awaitable sleep function
1423
+ /// #[pyfunction]
1424
+ /// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
1425
+ /// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
1426
+ /// let secs = Rc::new(secs);
1427
+ ///
1428
+ /// pyo3_asyncio::generic::local_cancellable_future_into_py::<MyCustomRuntime, _>(
1429
+ /// py,
1430
+ /// async move {
1431
+ /// MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
1432
+ /// Python::with_gil(|py| Ok(py.None()))
1433
+ /// }
1434
+ /// )
1435
+ /// }
1436
+ /// ```
1437
+ pub fn local_cancellable_future_into_py < R , F > ( py : Python , fut : F ) -> PyResult < & PyAny >
1438
+ where
1439
+ R : Runtime + SpawnLocalExt ,
1440
+ F : Future < Output = PyResult < PyObject > > + ' static ,
1441
+ {
1442
+ local_cancellable_future_into_py_with_loop :: < R , F > ( get_current_loop :: < R > ( py) ?, fut)
1443
+ }
0 commit comments