Skip to content

Commit dcb073b

Browse files
authored
Merge pull request #7 from awestlake87/simplify-into-future
Simplify `into_future`
2 parents 85ff67f + 5fb84e5 commit dcb073b

File tree

5 files changed

+13
-16
lines changed

5 files changed

+13
-16
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ fn main() {
4242
Python::with_gil(|py| {
4343
// convert asyncio.sleep into a Rust Future
4444
pyo3_asyncio::into_future(
45-
py,
4645
asyncio.call_method1(
4746
py,
4847
"sleep",

pytests/common/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub(super) fn test_into_future(
2121
Ok(async move {
2222
Python::with_gil(|py| {
2323
pyo3_asyncio::into_future(
24-
py,
2524
test_mod
2625
.call_method1(py, "py_sleep", (1.into_py(py),))?
2726
.as_ref(py),

pytests/test_async_std_asyncio.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn test_into_coroutine(
4040
Ok(async move {
4141
Python::with_gil(|py| {
4242
pyo3_asyncio::into_future(
43-
py,
4443
test_mod
4544
.call_method1(py, "sleep_for_1s", (sleeper_mod.getattr(py, "sleep_for")?,))?
4645
.as_ref(py),
@@ -60,7 +59,7 @@ fn test_async_sleep<'p>(
6059
task::sleep(Duration::from_secs(1)).await;
6160

6261
Python::with_gil(|py| {
63-
pyo3_asyncio::into_future(py, asyncio.as_ref(py).call_method1("sleep", (1.0,))?)
62+
pyo3_asyncio::into_future(asyncio.as_ref(py).call_method1("sleep", (1.0,))?)
6463
})?
6564
.await?;
6665

pytests/tokio_asyncio/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fn test_into_coroutine(
3737
Ok(async move {
3838
Python::with_gil(|py| {
3939
pyo3_asyncio::into_future(
40-
py,
4140
test_mod
4241
.call_method1(py, "sleep_for_1s", (sleeper_mod.getattr(py, "sleep_for")?,))?
4342
.as_ref(py),
@@ -57,7 +56,7 @@ fn test_async_sleep<'p>(
5756
tokio::time::sleep(Duration::from_secs(1)).await;
5857

5958
Python::with_gil(|py| {
60-
pyo3_asyncio::into_future(py, asyncio.as_ref(py).call_method1("sleep", (1.0,))?)
59+
pyo3_asyncio::into_future(asyncio.as_ref(py).call_method1("sleep", (1.0,))?)
6160
})?
6261
.await?;
6362

src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use std::future::Future;
9797

9898
use futures::channel::oneshot;
9999
use once_cell::sync::OnceCell;
100-
use pyo3::{exceptions::PyKeyboardInterrupt, prelude::*};
100+
use pyo3::{exceptions::PyKeyboardInterrupt, prelude::*, PyNativeType};
101101

102102
/// Test README
103103
#[doc(hidden)]
@@ -299,11 +299,15 @@ impl PyTaskCompleter {
299299
}
300300
}
301301

302-
/// Convert a Python coroutine into a Rust Future
302+
/// Convert a Python `awaitable` into a Rust Future
303+
///
304+
/// This function converts the `awaitable` into a Python Task using `run_coroutine_threadsafe`. A
305+
/// completion handler sends the result of this Task through a
306+
/// `futures::channel::oneshot::Sender<PyResult<PyObject>>` and the future returned by this function
307+
/// simply awaits the result through the `futures::channel::oneshot::Receiver<PyResult<PyObject>>`.
303308
///
304309
/// # Arguments
305-
/// * `py` - The current PyO3 GIL guard
306-
/// * `coro` - The Python coroutine to be converted
310+
/// * `awaitable` - The Python `awaitable` to be converted
307311
///
308312
/// # Examples
309313
///
@@ -334,7 +338,6 @@ impl PyTaskCompleter {
334338
///
335339
/// Python::with_gil(|py| {
336340
/// pyo3_asyncio::into_future(
337-
/// py,
338341
/// test_mod
339342
/// .call_method1(py, "py_sleep", (seconds.into_py(py),))?
340343
/// .as_ref(py),
@@ -344,16 +347,14 @@ impl PyTaskCompleter {
344347
/// Ok(())
345348
/// }
346349
/// ```
347-
pub fn into_future(
348-
py: Python,
349-
coro: &PyAny,
350-
) -> PyResult<impl Future<Output = PyResult<PyObject>> + Send> {
350+
pub fn into_future(awaitable: &PyAny) -> PyResult<impl Future<Output = PyResult<PyObject>> + Send> {
351+
let py = awaitable.py();
351352
let (tx, rx) = oneshot::channel();
352353

353354
let task = CREATE_TASK
354355
.get()
355356
.expect(EXPECT_INIT)
356-
.call1(py, (coro, get_event_loop(py)))?;
357+
.call1(py, (awaitable, get_event_loop(py)))?;
357358
let on_complete = PyTaskCompleter { tx: Some(tx) };
358359

359360
task.call_method1(py, "add_done_callback", (on_complete,))?;

0 commit comments

Comments
 (0)