0
点赞
收藏
分享

微信扫一扫

mac m2停止mysql报错:ERROR! MySQL server PID file could not be found!

8052cf60ff5c 2023-06-16 阅读 41

广告时间

nacos-sdk-rust-binding-py : nacos-sdk-rust binding for Python with PyO3.

py 包 -> https://pypi.org/project/nacos-sdk-rust-binding-py

客官,走过路过不要错过,买不买瞧一瞧也是好的。行过路过唔好错过啦👆~

缘起?冲突!

啊哈?哪有那么多理由!就是闲来无事,想试试 binding for Python …

的确不像 nacos-sdk-rust-binding-node 有实际的需求,但也偶尔在 Nacos 社区用户群里看到有人问 nacos-sdk-python 是否支持 gRPC ;虽也想提供些帮助,之前也了解有 PyO3 神器,但确实不懂 Python 这门语言。

这不是巧了吗这不是

也是偶然,近来转岗到新部门,恰逢上手新工作有个文档记录了黑屏操作的 py 脚本 😂 ;而我作为负责人刚好又要提供它给内部用户,若我不理解脚本干了啥那说不过去;唯有尝试看了看,emmmm 语法确实不难,简单易理解。

前些天阅览了这篇文章 「为 Databend Rust Driver 实现 Python Binding」,我可记着它,会有一日能实现 nacos-sdk-rust binding for Python

周末~闲来无事!开搞~

How? PyO3 + Maturin

– 摘自「为 Databend Rust Driver 实现 Python Binding」

除了「Databend Rust Driver」还借鉴 PyO3 examples wasmer-python 等些项目,以及「Python3 教程 | 菜鸟教程 」

由 maturin 初始化项目,仅需十来个提交完成基本可用的功能
nacos-sdk-rust-binding-py-commit

难点

  1. python 默认非 aysnc ?
    a. maturin 构建提示 Rust 的绑定代码不能写 async 方法!
    b. 若 async ,py 应该是要借助 asyncio 库来支持,没深入理解也暂时让使用成本低,故而启用了非 async 的 nacos-sdk-rust 。
  2. python 函数被 Rust 调用
    a. Nacos 配置监听、服务订阅,这些 callback 函数需要由 py 用户实现逻辑作为参数传入。

第二点在 wasmer-python/packages/api/src/externals/function.rs 找到了借鉴,所以 Nacos Config Listener 绑定方法实现如下

#[pyo3(signature = (data_id, group, listener))]
pub fn add_listener(
    &self,
    py: Python,
    data_id: String,
    group: String,
    listener: &PyAny, // PyFunction arg: <NacosConfigResponse>
) -> PyResult<()> {
    if !listener.is_callable() {
        return Err(PyErr::new::<PyValueError, _>(
            "Arg `listener` must be a callable",
        ));
    }
    self.inner
        .add_listener(
            data_id,
            group,
            Arc::new(NacosConfigChangeListener {
                func: Arc::new(listener.to_object(py)),
            }),
        )
        .map_err(|nacos_err| PyRuntimeError::new_err(format!("{:?}", &nacos_err)))?;
    Ok(())
}

pub struct NacosConfigChangeListener {
    func: Arc<PyObject>,
}

impl nacos_sdk::api::config::ConfigChangeListener for NacosConfigChangeListener {
    fn notify(&self, config_resp: nacos_sdk::api::config::ConfigResponse) {
        let ffi_conf_resp = transfer_conf_resp(config_resp);

        // call PyFunction with args
        let _ = Python::with_gil(|py| -> PyResult<()> {
            let _ = self.func.call(py, (ffi_conf_resp,), None);
            Ok(())
        });
    }
}

总结

虽小编不是 python 真实用户,但闲来无事嘛,做点什么小贡献,亲身体验了「Rust could binding for Anythings!!!~」

  • https://github.com/napi-rs/napi-rs binding for NodeJs
  • https://github.com/PyO3/pyo3 binding for Python
  • https://github.com/dtolnay/cxx binding for C++
  • https://github.com/jni-rs/jni-rs binding for Java

附阅:「nacos-sdk-rust binding for NodeJs」、「Rust 从入门到放弃,再入门到贡献 nacos-sdk-rust」

举报

相关推荐

【MAC】M2 安装docker 与 mysql

0 条评论