Skip to content

Conversation

@kylebarron
Copy link
Contributor

  • The underlying get_opts returns a GetResult. So ideally it would be nice to return a Python object that wraps this GetResult so that in the future we could potentially stream a result into a Python AsyncIterator. However I don't think that would ever be possible because its bytes and into_stream consume self. And you can't have a Python method that consumes self.

    So I think the best approach is to currently return bytes and potentially in the future have a separate get_opts_stream method that returns an async iterator.

I had tried with

#[pyclass(name = "GetResult")]
#[derive(Debug)]
pub struct PyGetResult {
    rt: Arc<Runtime>,
    result: Arc<Mutex<GetResult>>,
}

#[pymethods]
impl PyGetResult {
    pub fn bytes(&self) -> PyResult<Cow<[u8]>> {
        let lock = self.rt.block_on(self.result.lock());
        let obj = self
            .rt
            .block_on(lock.bytes())
            .map_err(ObjectStoreError::from)?;
        Ok(Cow::Owned(obj.to_vec()))
    }

    pub fn bytes_async<'a>(&'a self, py: Python<'a>) -> PyResult<&PyAny> {
        pyo3_asyncio::tokio::future_into_py(py, async move {
            let obj = self
                .result
                .lock()
                .await
                .bytes()
                .await
                .map_err(ObjectStoreError::from)?;
            Ok(Cow::<[u8]>::Owned(obj.to_vec()))
        })
    }
}

This is implemented on top of #6, and is expected to be reviewed after that.

@kylebarron
Copy link
Contributor Author

Closing because I implemented this in https://github.com/developmentseed/object-store-rs

@kylebarron kylebarron closed this Oct 18, 2024
@kylebarron kylebarron mentioned this pull request Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant