Skip to content

Commit a3a7217

Browse files
author
Andrew J Westlake
committed
Slowly updating some docs on deprecations and 0.15 alternatives
1 parent 2821340 commit a3a7217

File tree

7 files changed

+54
-101
lines changed

7 files changed

+54
-101
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,5 @@ fn test_contextvars() -> PyResult<()> {
319319
fn main() -> pyo3::PyResult<()> {
320320
pyo3::prepare_freethreaded_python();
321321

322-
Python::with_gil(|py| {
323-
// into_coroutine requires the 0.13 API
324-
pyo3_asyncio::try_init(py)?;
325-
pyo3_asyncio::async_std::run(py, pyo3_asyncio::testing::main())
326-
})
322+
Python::with_gil(|py| pyo3_asyncio::async_std::run(py, pyo3_asyncio::testing::main()))
327323
}

pytests/test_tokio_current_thread_asyncio.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ fn main() -> pyo3::PyResult<()> {
88
pyo3::prepare_freethreaded_python();
99

1010
Python::with_gil(|py| {
11-
// into_coroutine requires the 0.13 API
12-
pyo3_asyncio::try_init(py)?;
13-
1411
let mut builder = tokio::runtime::Builder::new_current_thread();
1512
builder.enable_all();
1613

pytests/test_tokio_multi_thread_asyncio.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ use pyo3::prelude::*;
77
fn main() -> pyo3::PyResult<()> {
88
pyo3::prepare_freethreaded_python();
99

10-
Python::with_gil(|py| {
11-
// into_coroutine requires the 0.13 API
12-
pyo3_asyncio::try_init(py)?;
13-
pyo3_asyncio::tokio::run(py, pyo3_asyncio::testing::main())
14-
})
10+
Python::with_gil(|py| pyo3_asyncio::tokio::run(py, pyo3_asyncio::testing::main()))
1511
}

src/async_std.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,9 @@ where
284284

285285
/// Convert a Rust Future into a Python awaitable
286286
///
287-
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
288-
/// the `asyncio.Future` is cancelled from Python.
289-
///
290-
/// __This function will be deprecated in favor of [`future_into_py_with_loop`] in `v0.15` because
291-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
292-
/// replaced with [`future_into_py_with_loop`].__
287+
/// __This function was deprecated in favor of [`future_into_py_with_locals`] in `v0.15` because
288+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
289+
/// replaced with [`future_into_py_with_locals`].__
293290
///
294291
/// # Arguments
295292
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -363,8 +360,8 @@ where
363360
/// Unlike [`future_into_py`], this function will stop the Rust future from running when
364361
/// the `asyncio.Future` is cancelled from Python.
365362
///
366-
/// __This function will be deprecated in favor of [`future_into_py`] in `v0.15` because
367-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
363+
/// __This function was deprecated in favor of [`future_into_py`] in `v0.15` because
364+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
368365
/// replaced with [`future_into_py`].__
369366
///
370367
/// # Arguments
@@ -509,12 +506,9 @@ where
509506

510507
/// Convert a `!Send` Rust Future into a Python awaitable
511508
///
512-
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running when
513-
/// the `asyncio.Future` is cancelled from Python.
514-
///
515-
/// __This function will be deprecated in favor of [`local_future_into_py_with_loop`] in `v0.15` because
516-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
517-
/// replaced with [`local_future_into_py_with_loop`].__
509+
/// __This function was deprecated in favor of [`local_future_into_py_with_locals`] in `v0.15` because
510+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
511+
/// replaced with [`local_future_into_py_with_locals`].__
518512
///
519513
/// # Arguments
520514
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -618,8 +612,8 @@ where
618612
/// Unlike [`local_future_into_py`], this function will stop the Rust future from running when
619613
/// the `asyncio.Future` is cancelled from Python.
620614
///
621-
/// __This function will be deprecated in favor of [`local_future_into_py`] in `v0.15` because
622-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
615+
/// __This function was deprecated in favor of [`local_future_into_py`] in `v0.15` because
616+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
623617
/// replaced with [`local_future_into_py`].__
624618
///
625619
/// # Arguments

src/generic.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub trait SpawnLocalExt: Runtime {
4444

4545
/// Exposes the utilities necessary for using task-local data in the Runtime
4646
pub trait ContextExt: Runtime {
47-
/// Set the task local event loop for the given future
47+
/// Set the task locals for the given future
4848
fn scope<F, R>(locals: TaskLocals, fut: F) -> Pin<Box<dyn Future<Output = R> + Send>>
4949
where
5050
F: Future<Output = R> + Send + 'static;
@@ -55,7 +55,7 @@ pub trait ContextExt: Runtime {
5555

5656
/// Adds the ability to scope task-local data for !Send futures
5757
pub trait LocalContextExt: Runtime {
58-
/// Set the task local event loop for the given !Send future
58+
/// Set the task locals for the given !Send future
5959
fn scope_local<F, R>(locals: TaskLocals, fut: F) -> Pin<Box<dyn Future<Output = R>>>
6060
where
6161
F: Future<Output = R> + 'static;
@@ -310,9 +310,9 @@ fn set_result(event_loop: &PyAny, future: &PyAny, result: PyResult<PyObject>) ->
310310

311311
/// Convert a Python `awaitable` into a Rust Future
312312
///
313-
/// This function simply forwards the future and the `event_loop` returned by [`get_current_loop`]
314-
/// to [`into_future_with_loop`](`crate::into_future_with_loop`). See
315-
/// [`into_future_with_loop`](`crate::into_future_with_loop`) for more details.
313+
/// This function simply forwards the future and the task locals returned by [`get_current_locals`]
314+
/// to [`into_future_with_locals`](`crate::into_future_with_locals`). See
315+
/// [`into_future_with_locals`](`crate::into_future_with_locals`) for more details.
316316
///
317317
/// # Arguments
318318
/// * `awaitable` - The Python `awaitable` to be converted
@@ -422,6 +422,7 @@ where
422422
/// Convert a Rust Future into a Python awaitable with a generic runtime
423423
///
424424
/// # Arguments
425+
/// * `py` - PyO3 GIL guard
425426
/// * `locals` - The task-local data for Python
426427
/// * `fut` - The Rust future to be converted
427428
///
@@ -579,6 +580,8 @@ where
579580

580581
/// Convert a Rust Future into a Python awaitable with a generic runtime
581582
///
583+
/// __
584+
///
582585
/// # Arguments
583586
/// * `event_loop` - The Python event loop that the awaitable should be attached to
584587
/// * `fut` - The Rust future to be converted
@@ -760,12 +763,11 @@ impl PyDoneCallback {
760763

761764
/// Convert a Rust Future into a Python awaitable with a generic runtime
762765
///
763-
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
764-
/// the `asyncio.Future` is cancelled from Python.
766+
/// __This function was deprecated in favor of [`future_into_py_with_locals`] in `v0.15` because
767+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
768+
/// replaced with [`future_into_py_with_locals`].__
765769
///
766-
/// __This function will be deprecated in favor of [`future_into_py_with_loop`] in `v0.15` because
767-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
768-
/// replaced with [`future_into_py_with_loop`].__
770+
/// __In `v0.16` this function will be removed__
769771
///
770772
/// # Arguments
771773
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -954,13 +956,12 @@ where
954956

955957
/// Convert a Rust Future into a Python awaitable with a generic runtime
956958
///
957-
/// Unlike [`future_into_py`], this function will stop the Rust future from running when the
958-
/// `asyncio.Future` is cancelled from Python.
959-
///
960-
/// __This function will be deprecated in favor of [`future_into_py`] in `v0.15` because
961-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
959+
/// __This function was deprecated in favor of [`future_into_py`] in `v0.15` because
960+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
962961
/// replaced with [`future_into_py`].__
963962
///
963+
/// __In `v0.16` this function will be removed__
964+
///
964965
/// # Arguments
965966
/// * `py` - The current PyO3 GIL guard
966967
/// * `fut` - The Rust future to be converted
@@ -1051,10 +1052,12 @@ where
10511052
future_into_py::<R, F, PyObject>(py, fut)
10521053
}
10531054

1054-
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
1055+
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime and manual
1056+
/// specification of task locals.
10551057
///
10561058
/// # Arguments
1057-
/// * `event_loop` - The Python event loop that the awaitable should be attached to
1059+
/// * `py` - PyO3 GIL guard
1060+
/// * `locals` - The task locals for the future
10581061
/// * `fut` - The Rust future to be converted
10591062
///
10601063
/// # Examples
@@ -1231,6 +1234,8 @@ where
12311234

12321235
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
12331236
///
1237+
/// __In `v0.16` this function will be removed__
1238+
///
12341239
/// # Arguments
12351240
/// * `event_loop` - The Python event loop that the awaitable should be attached to
12361241
/// * `fut` - The Rust future to be converted
@@ -1320,6 +1325,10 @@ where
13201325
/// )
13211326
/// }
13221327
/// ```
1328+
#[deprecated(
1329+
since = "0.15.0",
1330+
note = "Use pyo3_asyncio::generic::local_future_into_py_with_locals instead"
1331+
)]
13231332
pub fn local_future_into_py_with_loop<R, F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
13241333
where
13251334
R: Runtime + SpawnLocalExt + LocalContextExt,
@@ -1335,12 +1344,9 @@ where
13351344

13361345
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
13371346
///
1338-
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running
1339-
/// when the `asyncio.Future` is cancelled from Python.
1340-
///
1341-
/// __This function will be deprecated in favor of [`local_future_into_py_with_loop`] in `v0.15` because
1342-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1343-
/// replaced with [`local_future_into_py_with_loop`].__
1347+
/// __This function was deprecated in favor of [`local_future_into_py_with_locals`] in `v0.15` because
1348+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1349+
/// replaced with [`local_future_into_py_with_locals`].__
13441350
///
13451351
/// # Arguments
13461352
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -1435,6 +1441,7 @@ where
14351441
since = "0.15.0",
14361442
note = "Use pyo3_asyncio::generic::local_future_into_py_with_locals instead"
14371443
)]
1444+
#[allow(deprecated)]
14381445
pub fn local_cancellable_future_into_py_with_loop<R, F>(
14391446
event_loop: &PyAny,
14401447
fut: F,
@@ -1560,8 +1567,8 @@ where
15601567
/// Unlike [`local_future_into_py`], this function will stop the Rust future from running when the
15611568
/// `asyncio.Future` is cancelled from Python.
15621569
///
1563-
/// __This function will be deprecated in favor of [`local_future_into_py`] in `v0.15` because
1564-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1570+
/// __This function was deprecated in favor of [`local_future_into_py`] in `v0.15` because
1571+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
15651572
/// replaced with [`local_future_into_py`].__
15661573
///
15671574
/// # Arguments

src/lib.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,6 @@ static CONTEXTVARS: OnceCell<Option<PyObject>> = OnceCell::new();
356356
static ENSURE_FUTURE: OnceCell<PyObject> = OnceCell::new();
357357
static GET_RUNNING_LOOP: OnceCell<PyObject> = OnceCell::new();
358358

359-
static CACHED_EVENT_LOOP: OnceCell<PyObject> = OnceCell::new();
360-
static EXECUTOR: OnceCell<PyObject> = OnceCell::new();
361-
362359
fn ensure_future<'p>(py: Python<'p>, awaitable: &'p PyAny) -> PyResult<&'p PyAny> {
363360
ENSURE_FUTURE
364361
.get_or_try_init(|| -> PyResult<PyObject> {
@@ -391,40 +388,12 @@ fn close(event_loop: &PyAny) -> PyResult<()> {
391388
Ok(())
392389
}
393390

394-
/// Attempt to initialize the Python and Rust event loops
395-
///
396-
/// - Must be called before any other pyo3-asyncio functions.
397-
/// - Calling `try_init` a second time returns `Ok(())` and does nothing.
398-
/// > In future versions this may return an `Err`.
399-
#[deprecated(
400-
since = "0.14.0",
401-
note = "see the [migration guide](https://github.com/awestlake87/pyo3-asyncio/#migrating-from-013-to-014) for more details"
402-
)]
403-
pub fn try_init(py: Python) -> PyResult<()> {
404-
CACHED_EVENT_LOOP.get_or_try_init(|| -> PyResult<PyObject> {
405-
let event_loop = asyncio_get_event_loop(py)?;
406-
let executor = py
407-
.import("concurrent.futures.thread")?
408-
.call_method0("ThreadPoolExecutor")?;
409-
event_loop.call_method1("set_default_executor", (executor,))?;
410-
411-
EXECUTOR.set(executor.into()).unwrap();
412-
Ok(event_loop.into())
413-
})?;
414-
415-
Ok(())
416-
}
417-
418391
fn asyncio(py: Python) -> PyResult<&PyAny> {
419392
ASYNCIO
420393
.get_or_try_init(|| Ok(py.import("asyncio")?.into()))
421394
.map(|asyncio| asyncio.as_ref(py))
422395
}
423396

424-
fn asyncio_get_event_loop(py: Python) -> PyResult<&PyAny> {
425-
asyncio(py)?.call_method0("get_event_loop")
426-
}
427-
428397
/// Get a reference to the Python Event Loop from Rust
429398
///
430399
/// Equivalent to `asyncio.get_running_loop()` in Python 3.7+.

src/tokio.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,9 @@ where
301301

302302
/// Convert a Rust Future into a Python awaitable
303303
///
304-
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
305-
/// the `asyncio.Future` is cancelled from Python.
306-
///
307-
/// __This function will be deprecated in favor of [`future_into_py_with_loop`] in `v0.15` because
308-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
309-
/// replaced with [`future_into_py_with_loop`].__
304+
/// __This function was deprecated in favor of [`future_into_py_with_locals`] in `v0.15` because
305+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
306+
/// replaced with [`future_into_py_with_locals`].__
310307
///
311308
/// # Arguments
312309
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -380,8 +377,8 @@ where
380377
/// Unlike [`future_into_py`], this function will stop the Rust future from running when
381378
/// the `asyncio.Future` is cancelled from Python.
382379
///
383-
/// __This function will be deprecated in favor of [`future_into_py`] in `v0.15` because
384-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
380+
/// __This function was deprecated in favor of [`future_into_py`] in `v0.15` because
381+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
385382
/// replaced with [`future_into_py`].__
386383
///
387384
/// # Arguments
@@ -558,12 +555,9 @@ where
558555

559556
/// Convert a `!Send` Rust Future into a Python awaitable
560557
///
561-
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running when
562-
/// the `asyncio.Future` is cancelled from Python.
563-
///
564-
/// __This function will be deprecated in favor of [`local_future_into_py_with_loop`] in `v0.15` because
565-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
566-
/// replaced with [`local_future_into_py_with_loop`].__
558+
/// __This function was deprecated in favor of [`local_future_into_py_with_locals`] in `v0.15` because
559+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
560+
/// replaced with [`local_future_into_py_with_locals`].__
567561
///
568562
/// # Arguments
569563
/// * `event_loop` - The Python event loop that the awaitable should be attached to
@@ -701,8 +695,8 @@ where
701695
/// Unlike [`local_future_into_py`], this function will stop the Rust future from running when
702696
/// the `asyncio.Future` is cancelled from Python.
703697
///
704-
/// __This function will be deprecated in favor of [`local_future_into_py`] in `v0.15` because
705-
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
698+
/// __This function was deprecated in favor of [`local_future_into_py`] in `v0.15` because
699+
/// it became the default behaviour. In `v0.15`, any calls to this function can be seamlessly
706700
/// replaced with [`local_future_into_py`].__
707701
///
708702
/// # Arguments

0 commit comments

Comments
 (0)