Closed fcfangcc closed 2 years ago
Looks like the problem was not with the PyBytes type, but rather capturing the GIL guard py
in your async block. Here's what I was able to do to get this working:
lib.rs
use pyo3::prelude::*;
use pyo3::types::PyBytes;
#[pyclass]
pub struct A {}
#[pymethods]
impl A {
#[new]
pub fn new() -> Self {
Self {}
}
pub fn get_bytes<'p>(&self, py: Python<'p>) -> &'p PyBytes {
PyBytes::new(py, b"Hello, world!")
}
pub fn get_vecu8_async<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
pyo3_asyncio::tokio::future_into_py(py, async move { Ok(b"Hello, world!".to_vec()) })
}
pub fn get_bytes_async<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
// We need py here to create the future, but we do not want to capture py since it is !Send
//
// Instead, we want to create the future and release the GIL, then acquire the GIL in our
// future to create the bytes
pyo3_asyncio::tokio::future_into_py(py, async {
Python::with_gil(|py| Ok(PyBytes::new(py, b"Hello, world!").to_object(py)))
})
}
}
/// A Python module implemented in Rust.
#[pymodule]
fn testlib(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<A>()?;
Ok(())
}
test.py
import pyo3_bytes
import asyncio
import random
from testlib import A
async def test():
a= A()
print(a.get_bytes())
# return b"Hello, world!"
print(await a.get_vecu8_async())
# return [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
print(await a.get_bytes_async())
# i want it return b"Hello, world!"
asyncio.run(test())
It works,thanks
🌍 Environment
Your operating system and version:
Your python version: 3.10
How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: yes
Your Rust version (
rustc --version
): rustc 1.59.0 (9d1b2106e 2022-02-23)Your PyO3 version: 0.16.5
Have you tried using latest PyO3 master (replace
version = "0.x.y"
withgit = "https://github.com/awestlake87/pyo3-asyncio")?
: no💥 Reproducing
How to return a bytes type for Python use future_into_py, like method get_bytes_async.
create testlib with maturin
lib.rs
test.py
Please also write what exact flags are required to reproduce your results.