调用与构建 / 导出 Python 模块
导出 Python 模块
Aero 的杀手锏之一:一行命令把 Aero 代码变成 Python 可 import 的原生扩展。
用法
#[py_export]
fn aggregate(data: String, price_col: i64) -> i64 {
// ... 热循环
return total;
}构建:
aero build kernel.aero --pyext --py-module kernel产出 kernel.pyd(Windows)/ kernel.so,然后:
import kernel
print(kernel.aggregate(b"1,100\n2,200\n", 1))类型转换表
i64 | int | 格式 l(实际 L,long long) |
f64 | float | 格式 d |
bool | bool | 格式 p |
str | str | UTF-8,格式 s |
String | bytes | 格式 y# |
| 无返回 | None | — |
返回值:i64 → int、f64 → float、bool → bool、str → str、String → bytes。
Python 定位顺序
--py-home → PYTHON_HOME → 探测 python。
v1 锁定 CPython 完整 ABI:升级 Python 需重新编译。Windows 官方 Python 的 MSVC .lib 会被自动转成 MinGW 兼容的导入库(缓存在临时目录)。
真实案例:aero-csv
这就是 aero-csv 的做法——200 万行 CSV 聚合 aggregate() 约 51ms(39M 行/s),比纯 Python 快 11 倍、比 numpy 快 9 倍。整个热循环是 Aero 写的,Python 只留薄薄一层胶水。