如何使用python wasmtime调用rust生成的wasm库
作者:huzhenwei 时间:2023-11-26 23:58:38
本文介绍了使用python wasmtime来访问rust库的便捷方法,步骤极其简练,可以在生产环境中使用。
安装rust target wasm32-wasi
使用如下命令安装wasm32-wasi,用于便捷地将rust编译为wasm字节码。
rustup target add wasm32-wasi
编写rust库
使用cargo创建rust库
cd /mnt/d/workspace/rust
cargo new --lib wasmlib
编辑Cargo.toml文件
cd wasmlib
echo "[lib]" >> Cargo.toml
echo "crate-type = ['cdylib']" >> Cargo.toml
编辑rust源文件
编辑src/lib.rs
, 修改文件内容如下
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
将rust库编译为wasm字节码
将rust库编译为wasm字节码
cargo build --release --target wasm32-wasi
查看生成的wasm文件
cd target/wasm32-wasi/release
ls
结果如下,其中wasmlib.wasm就是wasm字节码文件:
build deps examples incremental wasmlib.d wasmlib.wasm
安装python wasmtime库
pip3 install wasmtime
测试使用python wasmtime调用rust库(wasm字节码)
hzw@hzwwin:/mnt/d/workspace/rust/wasmlib/target/wasm32-wasi/release$ python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wasmtime.loader
>>> import wasmlib # 不需要加.wasm扩展名
>>> dir(wasmlib)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'memory']
>>> wasmlib.add(2, 2)
4
>>> wasmlib.add(2, 4)
6
>>> wasmlib.add("a", "b")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hzw/.local/lib/python3.10/site-packages/wasmtime/loader.py", line 89, in <lambda>
item = lambda *args,func=func: func(store, *args) # noqa
File "/home/hzw/.local/lib/python3.10/site-packages/wasmtime/_func.py", line 83, in __call__
param_vals = [Val._convert(ty, params[i]) for i, ty in enumerate(param_tys)]
File "/home/hzw/.local/lib/python3.10/site-packages/wasmtime/_func.py", line 83, in <listcomp>
param_vals = [Val._convert(ty, params[i]) for i, ty in enumerate(param_tys)]
File "/home/hzw/.local/lib/python3.10/site-packages/wasmtime/_value.py", line 154, in _convert
raise TypeError("don't know how to convert %r to %s" % (val, ty))
TypeError: don't know how to convert 'a' to i32
参考链接
https://docs.wasmtime.dev/
https://pypi.org/project/wasmtime/
来源:https://blog.csdn.net/huzhenwei/article/details/128517154
标签:python,wasmtime,rust,wasm
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解python pandas 分组统计的方法
2021-01-30 05:33:48
![](https://img.aspxhome.com/file/2023/3/99673_0s.png)
linux下默认安装mysql数据库
2011-03-08 09:24:00
深入了解Python中Lambda函数的用法
2023-02-03 10:09:01
通过python实现弹窗广告拦截过程详解
2022-04-12 09:21:31
![](https://img.aspxhome.com/file/2023/8/71448_0s.png)
浅谈Python中的zip()与*zip()函数详解
2021-01-26 13:28:49
opencv基于Haar人脸检测和眼睛检测
2022-02-10 13:43:59
![](https://img.aspxhome.com/file/2023/1/90891_0s.jpg)
thinkPHP实现MemCache分布式缓存功能
2024-03-13 00:28:05
vue基本使用--refs获取组件或元素的实例
2024-05-02 16:35:20
完美解决vue中报错 “TypeError: Cannot read properties of null (reading'forEach')“
2023-07-02 17:06:29
![](https://img.aspxhome.com/file/2023/9/139929_0s.png)
PHP实现的AES双向加密解密功能示例【128位】
2023-09-30 14:52:51
python反编译教程之2048小游戏实例
2023-07-24 08:04:47
![](https://img.aspxhome.com/file/2023/5/71405_0s.png)
关于tf.reverse_sequence()简述
2022-05-23 03:05:08
关于Javascript闭包与应用的详解
2024-04-23 09:09:28
![](https://img.aspxhome.com/file/2023/6/135906_0s.png)
chatGPT deBug解决管理员登入服务器返回401问题
2022-08-07 06:38:09
![](https://img.aspxhome.com/file/2023/6/131806_0s.png)
Python编程调用百度API实现地理位置经纬度坐标转换示例
2023-12-30 11:44:38
![](https://img.aspxhome.com/file/2023/6/69566_0s.jpg)
Python星号*与**用法分析
2021-11-30 10:32:50
![](https://img.aspxhome.com/file/2023/2/131642_0s.png)
Vue中子组件调用父组件的3种方法实例
2024-05-13 09:08:18
python实现余弦相似度文本比较的示例
2023-01-18 09:17:48
![](https://img.aspxhome.com/file/2023/2/81462_0s.png)
MySQL8重置root账户密码图文教程超详细讲解
2024-01-25 17:40:55
![](https://img.aspxhome.com/file/2023/6/78106_0s.png)
python 操作 mongodb 数据库详情
2024-01-19 17:53:45