20
pyo3 async runtime (lemmy.physfluids.fr)

Hello,

I am trying to wrap some Rust code that uses sea-orm compiled with "runtime-tokio-rustls" feature. I am using the new pyo3 "experimental-async" feature, which AFAIK is based on pyo3_async.

So I get that this is supposed to be runtime-agnostic, and not have two event loops, one for python async and one for rust async.

But, how do I tell pyo3 to use tokio runtime specifically? Or, how do I set up Python asyncio eventloop to use tokio somehow?

Indeed, it seems that it does not use tokio runtime, so I get this error when I invoke the rust async function from Python:

pyo3_runtime.PanicException: this functionality requires a Tokio context
top 3 comments
sorted by: hot top controversial new old
[-] BB_C@programming.dev 3 points 7 months ago

I don't have an informed answer, and may be you know all of this already.

(I only used pyo3 once in one of my projects, and didn't know about this new experimental feature, I also only used it to call python code from Rust, which is the other half of the crate, so to speak.)

https://github.com/PyO3/pyo3/blob/main/guide/src/async-await.md

Python awaitables instantiated with this method can only be awaited in asyncio context. Other Python async runtime may be supported in the future.

So the runtime here is from the python side. And my first guess would be that support doesn't extend to use-cases where special (Rust) runtime support is needed, like the typical io (and sometimes time) features of the tokio runtime that are often used.

Maybe the reference to "Other Python async runtime" in the quote above is hinting at something that may support such use-cases in the future.

If your Rust code doesn't work without using one of the enable_() methods here, then, that's probably your answer.

[-] Giooschi@lemmy.world 3 points 7 months ago* (last edited 7 months ago)

If your goal is to just .await some future that require the Tokio runtime you can use the async-compat crate.

[-] BatmanAoD@programming.dev 3 points 7 months ago

I'm not familiar with pyo3_async, but I don't think you can force Python asyncio to use the Tokio event loop. This is discussed in the documentation of a different PyO3 async crate, pyo3-asyblncio: https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/#why-two-event-loops

this post was submitted on 08 Apr 2024
20 points (100.0% liked)

Rust

5960 readers
2 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS