Skip to content

[Feature Request] Upgrade PyO3 to latest and use pyo3-async-runtimes instead of pyo3-asyncio #816

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

Open
cretz opened this issue Apr 3, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@cretz
Copy link
Member

cretz commented Apr 3, 2025

Describe the solution you'd like

https://github.com/PyO3/pyo3-async-runtimes is now available as a successor to https://github.com/awestlake87/pyo3-asyncio, we should try to use it. This also may solve #300.

Note at

// Code below through the rest of the file is similar to
// https://github.com/awestlake87/pyo3-asyncio/blob/v0.16.0/src/tokio.rs but
// altered to support spawning based on current Tokio runtime instead of a
// single static one
pub(crate) struct TokioRuntime;
tokio::task_local! {
static TASK_LOCALS: std::cell::OnceCell<pyo3_asyncio::TaskLocals>;
}
impl pyo3_asyncio::generic::Runtime for TokioRuntime {
type JoinError = tokio::task::JoinError;
type JoinHandle = tokio::task::JoinHandle<()>;
fn spawn<F>(fut: F) -> Self::JoinHandle
where
F: Future<Output = ()> + Send + 'static,
{
tokio::runtime::Handle::current().spawn(fut)
}
}
impl pyo3_asyncio::generic::ContextExt for TokioRuntime {
fn scope<F, R>(
locals: pyo3_asyncio::TaskLocals,
fut: F,
) -> Pin<Box<dyn Future<Output = R> + Send>>
where
F: Future<Output = R> + Send + 'static,
{
let cell = std::cell::OnceCell::new();
cell.set(locals).unwrap();
Box::pin(TASK_LOCALS.scope(cell, fut))
}
fn get_task_locals() -> Option<pyo3_asyncio::TaskLocals> {
TASK_LOCALS
.try_with(|c| c.get().cloned())
.unwrap_or_default()
}
}
we had to copy over some pyo3-asyncio code because it only supported a spawning from a global Tokio runtime and we needed to spawn from the current one. Make sure that pyo3-async-runtimes can handle this properly.

@cretz cretz added the enhancement New feature or request label Apr 3, 2025
@cretz
Copy link
Member Author

cretz commented Apr 3, 2025

See https://community.temporal.io/t/support-for-graalpy/17029 for context on why this was created. Also see related issue at oracle/graalpython#494 that encouraged this to be opened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant