PyO3 Async Runtimes 项目启动与配置教程

PyO3 Async Runtimes 项目启动与配置教程

pyo3-async-runtimes PyO3-based bridges between Python and Rust async runtimes pyo3-async-runtimes 项目地址: https://gitcode.com/gh_mirrors/py/pyo3-async-runtimes

1. 项目目录结构及介绍

PyO3 Async Runtimes 项目目录结构如下:

pyo3-async-runtimes/
├── .devcontainer/
├── .githooks/
├── .github/
├── examples/
├── pyo3-async-runtimes-macros/
├── pytests/
├── src/
├── .gitignore
├── CHANGELOG.md
├── Cargo.toml
├── Code-of-Conduct.md
├── Contributing.md
├── LICENSE
├── Makefile
├── README.md
├── codecov.yml
  • .devcontainer/:开发容器配置文件
  • .githooks/:Git 钩子配置文件
  • .github/:GitHub Action 配置文件
  • examples/:示例代码
  • pyo3-async-runtimes-macros/:PyO3 Async Runtimes 宏定义
  • pytests/:Python 测试代码
  • src/:源代码
  • .gitignore:Git 忽略文件
  • CHANGELOG.md:更改日志
  • Cargo.toml:Rust 项目配置文件
  • Code-of-Conduct.md:行为准则
  • Contributing.md:贡献指南
  • LICENSE:开源许可证
  • Makefile:Makefile 脚本
  • README.md:项目介绍
  • codecov.yml:CodeCov 配置文件

2. 项目的启动文件介绍

项目启动文件为 src/main.rs。该文件定义了一个异步函数,通过 pyo3-async-runtimes 库将 Python 的 asyncio.sleep 函数转换为 Rust 的 Future,并在 Rust 的异步运行时中执行。

use pyo3::prelude::*;
use pyo3_async_runtimes::tokio::main;

#[tokio::main]
async fn main() -> PyResult<()> {
    let fut = Python::with_gil(|py| {
        let asyncio = py.import("asyncio").unwrap();
        pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.0.into_pyobject(py).unwrap(),)).unwrap())
    }).unwrap();
    fut.await?;
    Ok(())
}

3. 项目的配置文件介绍

项目配置文件为 Cargo.toml。该文件定义了项目的依赖、版本、特性等信息。

[package]
name = "pyo3-async-runtimes"
version = "0.25"
edition = "2018"

[dependencies]
pyo3 = { version = "0.25", features = ["attributes", "tokio-runtime"] }
tokio = "1.40"

[features]
default = ["attributes", "tokio-runtime"]
async-std-runtime = ["pyo3/async-std-runtime"]
tokio-runtime = ["pyo3/tokio-runtime"]

在配置文件中,可以设置项目依赖的版本、特性等信息。例如,如果要使用 async-std 运行时,可以取消注释 async-std-runtime 特性。

以上是 PyO3 Async Runtimes 项目的启动和配置教程,希望对您有所帮助。

pyo3-async-runtimes PyO3-based bridges between Python and Rust async runtimes pyo3-async-runtimes 项目地址: https://gitcode.com/gh_mirrors/py/pyo3-async-runtimes

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雷柏烁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值