Skip to content

Commit 64f08c8

Browse files
author
Andrew J Westlake
committed
Deprecated with_loop variants
1 parent 2b110e5 commit 64f08c8

File tree

6 files changed

+157
-39
lines changed

6 files changed

+157
-39
lines changed

pytests/common/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{thread, time::Duration};
22

33
use pyo3::prelude::*;
4+
use pyo3_asyncio::TaskLocals;
45

56
pub(super) const TEST_MOD: &'static str = r#"
67
import asyncio
@@ -17,8 +18,8 @@ pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> {
1718
let test_mod =
1819
PyModule::from_code(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;
1920

20-
pyo3_asyncio::into_future_with_loop(
21-
event_loop.as_ref(py),
21+
pyo3_asyncio::into_future_with_locals(
22+
&TaskLocals::new(event_loop.as_ref(py)),
2223
test_mod.call_method1("py_sleep", (1.into_py(py),))?,
2324
)
2425
})?;
@@ -61,7 +62,7 @@ pub(super) async fn test_other_awaitables(event_loop: PyObject) -> PyResult<()>
6162
),
6263
)?;
6364

64-
pyo3_asyncio::into_future_with_loop(event_loop.as_ref(py), task)
65+
pyo3_asyncio::into_future_with_locals(&TaskLocals::new(event_loop.as_ref(py)), task)
6566
})?;
6667

6768
fut.await?;

pytests/test_async_std_asyncio.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,13 @@ async fn test_cancel() -> PyResult<()> {
180180

181181
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
182182
let completed = Arc::clone(&completed);
183-
Ok(
184-
pyo3_asyncio::async_std::cancellable_future_into_py(py, async move {
185-
async_std::task::sleep(Duration::from_secs(1)).await;
186-
*completed.lock().unwrap() = true;
183+
Ok(pyo3_asyncio::async_std::future_into_py(py, async move {
184+
async_std::task::sleep(Duration::from_secs(1)).await;
185+
*completed.lock().unwrap() = true;
187186

188-
Ok(Python::with_gil(|py| py.None()))
189-
})?
190-
.into(),
191-
)
187+
Ok(Python::with_gil(|py| py.None()))
188+
})?
189+
.into())
192190
})?;
193191

194192
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
@@ -228,15 +226,13 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
228226

229227
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
230228
let completed = Arc::clone(&completed);
231-
Ok(
232-
pyo3_asyncio::async_std::cancellable_future_into_py(py, async move {
233-
async_std::task::sleep(Duration::from_secs(1)).await;
234-
*completed.lock().unwrap() = true;
235-
236-
Ok(Python::with_gil(|py| py.None()))
237-
})?
238-
.into(),
239-
)
229+
Ok(pyo3_asyncio::async_std::future_into_py(py, async move {
230+
async_std::task::sleep(Duration::from_secs(1)).await;
231+
*completed.lock().unwrap() = true;
232+
233+
Ok(Python::with_gil(|py| py.None()))
234+
})?
235+
.into())
240236
})?;
241237

242238
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {

pytests/tokio_asyncio/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,19 @@ fn test_local_future_into_py(event_loop: PyObject) -> PyResult<()> {
135135
Python::with_gil(|py| {
136136
let non_send_secs = Rc::new(1);
137137

138-
let py_future = pyo3_asyncio::tokio::local_future_into_py_with_loop(
139-
event_loop.as_ref(py),
138+
let py_future = pyo3_asyncio::tokio::local_future_into_py_with_locals(
139+
py,
140+
TaskLocals::new(event_loop.as_ref(py)),
140141
async move {
141142
tokio::time::sleep(Duration::from_secs(*non_send_secs)).await;
142143
Ok(Python::with_gil(|py| py.None()))
143144
},
144145
)?;
145146

146-
pyo3_asyncio::into_future_with_loop(event_loop.as_ref(py), py_future)
147+
pyo3_asyncio::into_future_with_locals(
148+
&TaskLocals::new(event_loop.as_ref(py)),
149+
py_future,
150+
)
147151
})?
148152
.await?;
149153

@@ -177,15 +181,13 @@ async fn test_cancel() -> PyResult<()> {
177181

178182
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
179183
let completed = Arc::clone(&completed);
180-
Ok(
181-
pyo3_asyncio::tokio::cancellable_future_into_py(py, async move {
182-
tokio::time::sleep(Duration::from_secs(1)).await;
183-
*completed.lock().unwrap() = true;
184+
Ok(pyo3_asyncio::tokio::future_into_py(py, async move {
185+
tokio::time::sleep(Duration::from_secs(1)).await;
186+
*completed.lock().unwrap() = true;
184187

185-
Ok(Python::with_gil(|py| py.None()))
186-
})?
187-
.into(),
188-
)
188+
Ok(Python::with_gil(|py| py.None()))
189+
})?
190+
.into())
189191
})?;
190192

191193
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
@@ -227,14 +229,12 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
227229
let completed = Arc::new(Mutex::new(false));
228230
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
229231
let completed = Arc::clone(&completed);
230-
Ok(
231-
pyo3_asyncio::tokio::local_cancellable_future_into_py(py, async move {
232-
tokio::time::sleep(Duration::from_secs(1)).await;
233-
*completed.lock().unwrap() = true;
234-
Ok(Python::with_gil(|py| py.None()))
235-
})?
236-
.into(),
237-
)
232+
Ok(pyo3_asyncio::tokio::local_future_into_py(py, async move {
233+
tokio::time::sleep(Duration::from_secs(1)).await;
234+
*completed.lock().unwrap() = true;
235+
Ok(Python::with_gil(|py| py.None()))
236+
})?
237+
.into())
238238
})?;
239239

240240
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {

src/async_std.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ where
276276
/// )
277277
/// }
278278
/// ```
279+
#[deprecated(
280+
since = "0.15.0",
281+
note = "Use pyo3_asyncio::async_std::future_into_py_with_locals instead"
282+
)]
279283
pub fn future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
280284
where
281285
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -316,6 +320,10 @@ where
316320
/// )
317321
/// }
318322
/// ```
323+
#[deprecated(
324+
since = "0.15.0",
325+
note = "Use pyo3_asyncio::async_std::future_into_py_with_locals instead"
326+
)]
319327
pub fn cancellable_future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
320328
where
321329
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -383,6 +391,10 @@ where
383391
/// })
384392
/// }
385393
/// ```
394+
#[deprecated(
395+
since = "0.15.0",
396+
note = "Use pyo3_asyncio::async_std::future_into_py instead"
397+
)]
386398
pub fn cancellable_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
387399
where
388400
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -431,6 +443,10 @@ where
431443
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
432444
/// # fn main() {}
433445
/// ```
446+
#[deprecated(
447+
since = "0.15.0",
448+
note = "Use pyo3_asyncio::async_std::local_future_into_py_with_locals instead"
449+
)]
434450
pub fn local_future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
435451
where
436452
F: Future<Output = PyResult<PyObject>> + 'static,
@@ -486,6 +502,10 @@ where
486502
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
487503
/// # fn main() {}
488504
/// ```
505+
#[deprecated(
506+
since = "0.15.0",
507+
note = "Use pyo3_asyncio::async_std::local_future_into_py_with_locals instead"
508+
)]
489509
pub fn local_cancellable_future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
490510
where
491511
F: Future<Output = PyResult<PyObject>> + 'static,
@@ -583,6 +603,10 @@ where
583603
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
584604
/// # fn main() {}
585605
/// ```
606+
#[deprecated(
607+
since = "0.15.0",
608+
note = "Use pyo3_asyncio::async_std::local_future_into_py instead"
609+
)]
586610
pub fn local_cancellable_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
587611
where
588612
F: Future<Output = PyResult<PyObject>> + 'static,

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,10 @@ pub fn into_future_with_locals(
858858
/// Ok(())
859859
/// }
860860
/// ```
861+
#[deprecated(
862+
since = "0.15.0",
863+
note = "Use pyo3_asyncio::into_future_with_locals instead"
864+
)]
861865
pub fn into_future_with_loop(
862866
event_loop: &PyAny,
863867
awaitable: &PyAny,

src/tokio.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ where
292292
/// )
293293
/// }
294294
/// ```
295+
#[deprecated(
296+
since = "0.15.0",
297+
note = "Use pyo3_asyncio::tokio::future_into_py_with_locals instead"
298+
)]
295299
pub fn future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
296300
where
297301
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -332,6 +336,10 @@ where
332336
/// )
333337
/// }
334338
/// ```
339+
#[deprecated(
340+
since = "0.15.0",
341+
note = "Use pyo3_asyncio::tokio::future_into_py_with_locals instead"
342+
)]
335343
pub fn cancellable_future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
336344
where
337345
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -399,6 +407,10 @@ where
399407
/// })
400408
/// }
401409
/// ```
410+
#[deprecated(
411+
since = "0.15.0",
412+
note = "Use pyo3_asyncio::tokio::future_into_py instead"
413+
)]
402414
pub fn cancellable_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
403415
where
404416
F: Future<Output = PyResult<PyObject>> + Send + 'static,
@@ -463,13 +475,86 @@ where
463475
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
464476
/// # fn main() {}
465477
/// ```
478+
#[deprecated(
479+
since = "0.15.0",
480+
note = "Use pyo3_asyncio::tokio::local_future_into_py_with_locals instead"
481+
)]
466482
pub fn local_future_into_py_with_loop<'p, F>(event_loop: &'p PyAny, fut: F) -> PyResult<&PyAny>
467483
where
468484
F: Future<Output = PyResult<PyObject>> + 'static,
469485
{
470486
generic::local_future_into_py_with_loop::<TokioRuntime, _>(event_loop, fut)
471487
}
472488

489+
/// Convert a `!Send` Rust Future into a Python awaitable
490+
///
491+
/// # Arguments
492+
/// * `event_loop` - The Python event loop that the awaitable should be attached to
493+
/// * `fut` - The Rust future to be converted
494+
///
495+
/// # Examples
496+
///
497+
/// ```
498+
/// use std::{rc::Rc, time::Duration};
499+
///
500+
/// use pyo3::prelude::*;
501+
///
502+
/// /// Awaitable non-send sleep function
503+
/// #[pyfunction]
504+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
505+
/// // Rc is non-send so it cannot be passed into pyo3_asyncio::tokio::future_into_py
506+
/// let secs = Rc::new(secs);
507+
///
508+
/// pyo3_asyncio::tokio::local_future_into_py_with_locals(
509+
/// py,
510+
/// pyo3_asyncio::tokio::get_current_locals(py)?,
511+
/// async move {
512+
/// tokio::time::sleep(Duration::from_secs(*secs)).await;
513+
/// Python::with_gil(|py| Ok(py.None()))
514+
/// }
515+
/// )
516+
/// }
517+
///
518+
/// # #[cfg(all(feature = "tokio-runtime", feature = "attributes"))]
519+
/// #[pyo3_asyncio::tokio::main]
520+
/// async fn main() -> PyResult<()> {
521+
/// let locals = Python::with_gil(|py| -> PyResult<_> {
522+
/// pyo3_asyncio::tokio::get_current_locals(py)
523+
/// })?;
524+
///
525+
/// // the main coroutine is running in a Send context, so we cannot use LocalSet here. Instead
526+
/// // we use spawn_blocking in order to use LocalSet::block_on
527+
/// tokio::task::spawn_blocking(move || {
528+
/// // LocalSet allows us to work with !Send futures within tokio. Without it, any calls to
529+
/// // pyo3_asyncio::tokio::local_future_into_py will panic.
530+
/// tokio::task::LocalSet::new().block_on(
531+
/// pyo3_asyncio::tokio::get_runtime(),
532+
/// pyo3_asyncio::tokio::scope_local(locals, async {
533+
/// Python::with_gil(|py| {
534+
/// let py_future = sleep_for(py, 1)?;
535+
/// pyo3_asyncio::tokio::into_future(py_future)
536+
/// })?
537+
/// .await?;
538+
///
539+
/// Ok(())
540+
/// })
541+
/// )
542+
/// }).await.unwrap()
543+
/// }
544+
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
545+
/// # fn main() {}
546+
/// ```
547+
pub fn local_future_into_py_with_locals<F>(
548+
py: Python,
549+
locals: TaskLocals,
550+
fut: F,
551+
) -> PyResult<&PyAny>
552+
where
553+
F: Future<Output = PyResult<PyObject>> + 'static,
554+
{
555+
generic::local_future_into_py_with_locals::<TokioRuntime, _>(py, locals, fut)
556+
}
557+
473558
/// Convert a `!Send` Rust Future into a Python awaitable
474559
///
475560
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running when
@@ -534,6 +619,10 @@ where
534619
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
535620
/// # fn main() {}
536621
/// ```
622+
#[deprecated(
623+
since = "0.15.0",
624+
note = "Use pyo3_asyncio::tokio::local_future_into_py_with_locals instead"
625+
)]
537626
pub fn local_cancellable_future_into_py_with_loop<'p, F>(
538627
event_loop: &'p PyAny,
539628
fut: F,
@@ -664,6 +753,10 @@ where
664753
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
665754
/// # fn main() {}
666755
/// ```
756+
#[deprecated(
757+
since = "0.15.0",
758+
note = "Use pyo3_asyncio::tokio::local_future_into_py instead"
759+
)]
667760
pub fn local_cancellable_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
668761
where
669762
F: Future<Output = PyResult<PyObject>> + 'static,

0 commit comments

Comments
 (0)