Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run async code from current class in pymethods #3

Open
szandara opened this issue Dec 4, 2023 · 3 comments
Open

How to run async code from current class in pymethods #3

szandara opened this issue Dec 4, 2023 · 3 comments

Comments

@szandara
Copy link

szandara commented Dec 4, 2023

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.

@wyfo
Copy link
Owner

wyfo commented Dec 4, 2023

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 PyO3/pyo3#1632

First PRs have been merged, and a few of them are still waiting review. The last one PyO3/pyo3#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 PyO3/pyo3#3609, which it's the next PR to be reviewed.

@npuichigo
Copy link

@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。

@shenjackyuanjie
Copy link

a little ping
is PyO3/pyo3#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

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

No branches or pull requests

4 participants