Ballerina:如何从 Ballerina 内部调用 python 代码

问题描述 投票:0回答:2

我有一个芭蕾舞女演员后端,正在运行一个 http 侦听器。当我收到请求时,想要调用一个 python 脚本来获取参数、绘制图表并将其另存为 png。然后将 png 路径返回到 ballerina 后端。

resource function get [string year]/[string round]/laps/[string driverName] () returns string|error {
  string graphPath = < call python code with args (year, round, driverName) >
  return graphPath
}

如果无法直接调用,请告诉我如何调用OS命令?这个答案似乎已经过时了。

python process ballerina
2个回答
5
投票

Ballerina 中的操作系统命令执行支持将通过

os:Exec()
功能在即将发布的 Swanlake Update 2 版本中提供。


0
投票

Ballerina OS 模块从 1.4.0 版本开始支持此功能。以下是执行命令行脚本的示例芭蕾舞演员代码:


import ballerina/io;
import ballerina/os;

public function main() returns error? {
    os:Process exec = check os:exec({
        value: "ls",
        arguments: ["-l"]
    });
    
    int status = check exec.waitForExit();
    io:println(string `Process exit with status: ${status}`);

    byte[] output = check exec.output(io:stdout);
    io:println(check string:fromBytes(output));
}

请参阅文档:https://central.ballerina.io/ballerina/os/latest

© www.soinside.com 2019 - 2024. All rights reserved.