繁体   English   中英

试图用Java运行多个python脚本

[英]Trying to run multiple python scripts in Java

我有一个名为generate_graphs.py的python脚本,它使用python库生成图形。 图表是我们使用内部数据向客户展示的趋势。

我正在尝试从Java运行脚本,但我没有看到任何运行的证据。 没有迹象显示日志显示它已运行,但我不确定这是脚本本身没有运行,还是它的exec方法的实现。

该脚本作为其进程的一部分将数据插入到数据库中,并且不插入任何内容。 但是,当单独从命令行运行脚本命令时,脚本运行完全正常。

这是mkyong.com使用的执行命令实现:

private String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}

这里的方法总共被调用了大约40次,大约每3秒一次:

/**
 * Runs a command to execute the generate_graph python script
 *
 * @param server_id
 */
public void generateGraph(List<String> list_name, String server_id, String email_addr, String report_str) {

    String generate_graph_cmd = "python2.7 generate_graphs.py --l '%s' --server_name '%s' --email_addr '%s'  --report_string '%s' --debug";
    //We want to remove the lm_ part from the server name
    String server_name = server_id.split("_")[1].replace("\'", "");
    String list_name_str = "";

    for (String name : list_name){
        list_name_str += name + ",";
    }
    //We want to remove the trailing comma left by the above loop
    if (list_name_str.length() > 1){
        list_name_str = list_name_str.substring(0, list_name_str.length() - 1);
    }


    generate_graph_cmd = String.format(generate_graph_cmd, list_name_str, server_name, email_addr, report_str);

try {

    System.out.println("[Py Output] " + executeCommand(generate_graph_cmd));

    } catch (Exception e) {
        e.printStackTrace();
    }
    log.debug("Generating graph with the following parameters:\nserver_id: " + server_id + "\nlist_id: " + list_name.toString());
}

我只在日志中看到输出的log.debug部分。 我打电话太快/不正确吗? 任何帮助将不胜感激,谢谢!

我最终使用Apache Common的Exec来解决我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM