Skip to content

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

Closed
@cretz

Description

@cretz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions