pyo3-asyncio icon indicating copy to clipboard operation
pyo3-asyncio copied to clipboard

Non send types like PyDateTime

Open glennpierce opened this issue 3 years ago • 2 comments

If I have a pyfunction like

#[pyfunction]
fn get_data(py: Python, sensor_id: i32, start_datetime: &PyDateTime) -> PyResult<PyObject>  ...

that calls into_coroutine for a async block I get

F: Future<Output = PyResult<PyObject>> + Send + 'static, | ---- required by this bound in pyo3_asyncio::tokio::into_coroutine | = help: within PyDateTime, the trait Sync is not implemented for UnsafeCell<pyo3::ffi::PyObject>

Can values that are not send be wrapped in Arc or something. What is the usual way of dealing with that.

glennpierce avatar Jul 21 '21 08:07 glennpierce

Actually I converted it to a DateTime outside the into_coroutine which solves the issue. I guess there may be times where that won't do so original question may stand.

Thanks

glennpierce avatar Jul 21 '21 08:07 glennpierce

You might get this error when your future is capturing a reference to a PyO3 object like &PyDateTime when the real issue is that the future will not be able to capture that reference due to lifetime issues. Usually, the best thing to do is convert that &PyDateTime into a Py<PyDateTime> so it can be passed around like an Arc with the `'static' lifetime.

If that doesn't fix your Send issues, there are !Send variants of into_coroutine called local_future_into_py although they do have some caveats mentioned in this thread #23.

awestlake87 avatar Jul 21 '21 18:07 awestlake87