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

How to run async code from current class in pymethods

Open szandara opened this issue 2 years ago • 3 comments

From your example we can run async code like this. However, it's not clear to me how I can invoke async functions from the same class. Image I have to call another async method from this, how would I do that?

impl Counter {
     async fn incr_async(self_: pyo3::Py<Self>) -> pyo3::PyResult<usize> {
        pyo3::Python::with_gil(|gil| {
            let mut this = self_.borrow_mut(gil);
            this.0 += 1;
             Ok(this.0)
       })
   }
}

Example.

impl Counter {
     async fn incr_async(self_: pyo3::Py<Self>) -> pyo3::PyResult<usize> {
        pyo3::Python::with_gil(|gil| {
            let mut this = self_.borrow_mut(gil);
            // This does not compile because the context is sync.
            Ok(this.another_sync_method().await)
       })
   }
}

Also, I cannot return a future here because the gil lifetime will expire. I can use the async_std::task::block_on inside the GIL block but it would be nice to have something cleaner that keeps living on the same async loop.

szandara avatar Dec 04 '23 11:12 szandara

Actually, this crate was a POC, and I should be archived and deprecated when I will find the time for that. However, the good news is that I'm directly implementing async support in PyO3, based on this POC! See https://github.com/PyO3/pyo3/issues/1632

First PRs have been merged, and a few of them are still waiting review. The last one https://github.com/PyO3/pyo3/pull/3613 corresponds to expected final state of async support, with exciting features like PyFuture (not available in this crate).

So, if you can, I suggest you to use the branch of the linked PR for your PyO3 version. By the way, the feature you need is implemented in https://github.com/PyO3/pyo3/pull/3609, which it's the next PR to be reviewed.

wyfo avatar Dec 04 '23 12:12 wyfo

@wyfo I guess I'm a bit lucky. I just want to write a async data loading framework for deep learning today. After searching all day, I find your promising work. Looking forward to using it soon。

npuichigo avatar Dec 08 '23 08:12 npuichigo

a little ping is https://github.com/PyO3/pyo3/pull/3609 useable on stable ( 0.20.2 ) and released on pyo3? I still got a

error: `async fn` is not yet supported for Python functions.

       Additional crates such as `pyo3-asyncio` can be used to integrate async Rust and Python. For more information, see https://github.com/PyO3/pyo3/issues/1632
   --> src\py\class.rs:162:9
    |
162 |     pub async fn send_message(&self, message: &SendMessagePy) -> bool {
    |         ^^^^^

error: could not compile `ica-rs` (bin "ica-rs" test) due to 1 previous error

when try to write some async code

shenjackyuanjie avatar Feb 21 '24 17:02 shenjackyuanjie