pyo3-asyncio & references #4235
-
Been scratching my head on whether the bellow is feasible or not for a while now. Read a couple of similar questions both here and in I have an async function defined elsewhere that accepts // external_lib
async fn async_echo_string_ref(arg: &str) -> String {
arg.to_string()
}
// lib
#[pyfunction]
pub fn async_echo_string_ref(py: Python, input: PyObject) -> PyResult<Bound<PyAny>> {
let _input = input.extract(py)?;
pyo3_asyncio::tokio::future_into_py(py, async move {
let _res = external_lib::async_echo_string_ref(_input).await;
Python::with_gil(|gil| Ok(_res.into_py(gil)))
})
} which generates this error:
I understand that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Sorry for the slow reply. The general problem here is that extraction of data from Python types will need the If you're using Fortunately in PyO3 0.21 we also added I think it's probably time that the PyO3 organisation helped maintain |
Beta Was this translation helpful? Give feedback.
Sorry for the slow reply. The general problem here is that extraction of data from Python types will need the
py
token to pull the data out safely, but in cases like strings the immutability on the Python side means we don't actually need to have thepy
token once we've done the extraction.If you're using
pyo3-asyncio
, that presumably means you're still on PyO3 0.20 and the.extract()
function for&str
will tie the&str
to the lifetime ofpy
unnecessarily. After migrating off thegil-refs
API in 0.21, that connection is broken but instead you will be borrowing frominput
which leads to similar complications.Fortunately in PyO3 0.21 we also added
PyBackedStr
which fit exactly what you ne…