PyO3 / PyO3/pyo3

Issues with async support / Tokio

Open
#5,176 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

I attempt to compile the following code with experimental-async feature enabled:

#[pyclass]
struct Sender {
    _inner: IPCSender
}

#[pymethods]
impl Sender {
    #[new]
    async fn new(printname: String) -> PyResult<Self> {
        let ipcsender = IPCSender::new(&printname).await?;

        Ok(Self { _inner: ipcsender })
    }
}

#[pymodule]
fn python_ipc(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    m.add_class::<Sender>()?;
    Ok(())
}

However, I get this error message:

error[E0599]: no method named `convert` found for opaque type `impl Future<Output = Result<Sender, PyErr>>` in the current scope
  --> src/lib.rs:11:1
   |
11 | #[pymethods]
   | ^^^^^^^^^^^^ method not found in `impl Future<Output = Result<Sender, PyErr>>`
   |
   = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

I tried adding:

    static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RT.get_or_init(|| tokio::runtime::Runtime::new().expect("Unable to start Tokio runtime"));

to python_ipc function, but it still doesn't work.
IPCSender is a struct from a local crate, which uses tokio runtime.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report from src/lib.rs with the experimental-async feature and the async #[new] method shown. Start by inspecting how the #[pymethods] macro handles async constructors and compare that with async methods. Done means establishing whether this constructor pattern is supported and resolving or documenting the limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.