整合Python3.10 和 JDK1.8 docker镜像合成;java 调用python

​ 



准备
-rw-r--r-- 1 root root     573 2月  22 13:20 Dockerfile
-rw-r--r-- 1 root root 2638275 2月  22 13:18 eva-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root    2381 2月  22 13:18 pascal2labelme.py
-rw-r--r-- 1 root root      60 2月  22 13:22 requirements.txt
-rw-r--r-- 1 root root   11312 2月  22 13:18 检测算法评估.py
=============================================DockerfilePy=====================================
FROM python:3.10

ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8

WORKDIR /usr

RUN mkdir /usr/local/java

# ADD 是对相对路径jar, 把java添加到容器中
ADD OpenJDK8U-jdk_x64_linux_8u332b09.tar.gz /usr/local/java/

# 配置java环境变量
ENV JAVA_HOME /usr/local/java/openjdk-8u332-b09
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
=============================================DockerfilePy=====================================


生成镜像
docker build -f DockerfilePy -t jdk1.8_py3.10:v1 .

保存镜像到本地
docker save -o eva_py_11.tar jdk1.8_py3.10:v1

机器之间复制
scp eva_py_11.tar  root@192.168.xx.xx:/dubhe/data/business/ev-edge/storage/app/public/evaluation1
xxxxx

加载镜像
docker load -i eva_py_11.tar


推送到远程仓库
docker build -f DockerfilePy -t reg.xxxx.com/dev/jdk1.8_py3.10:v1 .
docker push reg.xxxx.com/dev/jdk1.8_py3.10:v1



----------------------------------Dockerfile_service--------------------------------------------------
FROM reg.xxxx.com/dev/jdk1.8_py3.10:v1

ENV LANG=C.UTF-8
ENV TZ=Asia/Shanghai

WORKDIR /var/www/app

COPY requirements.txt /var/www/app
COPY pascal2labelme.py /var/www/app
COPY 检测算法评估.py /var/www/app

COPY eva-1.0-SNAPSHOT.jar /var/www/app

RUN pip install -r /var/www/app/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

#ADD eva-1.0-SNAPSHOT.jar  /opt/eva.jar

#环境变量 JAVA_OPTS DEBUG_OPTS JMX_OPTS SPRING_CONFIG 由k8s传入
ENTRYPOINT ["sh", "-c", "java -jar /var/www/app/eva-1.0-SNAPSHOT.jar $Req_Params -Xms2048m -Xmx2048m -Xmn1024m" ]

----------------------------------Dockerfile_service--------------------------------------------------
#docker build -f Dockerfile -t eva_py_12 .

推送到远程仓库
docker build -f Dockerfile_service -t reg.xxxx.com/dev/evaluation:v1 .
docker push reg.xxxx.com/dev/evaluation:v1

docker build -f Dockerfile_service -t evaluation:local .
docker save -o evaluation1_local.tar evaluation:local
scp evaluation1_local.tar root@192.168.xx.xx:/dubhe/data/business/ev-edge/storage/app/public/evaluation2
xxxx@2023

加载镜像
docker load -i eva_py_11.tar








-----------------------------evaluation1----------------------------------evaluation1--------------------

/dubhe/data/business/ev-edge/storage/app/public/evaluation1/  这个是指定文件夹
   -- 标注数据
   -- 样本
   -- 样本结果

其中手动把
       样本结果中的白天、黑天的文件 复制到 综合
       标注数据中的白天json、夜晚json 复制到 综合json


 
docker run -e Req_Params='/var/www/app 192.168.xx.xx:19300 104 1' -v /dubhe/data/business/ev-edge/storage/app/public/evaluation1:/var/www/app eva-py:v11

​
package com.xxx.xx.evaluating;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class PythonUtil {


    public static Map<String, JSONArray> pyResultMap = new HashMap<>();
    public static String error = "error";

    public static void exec(String... args) {
        try {
            //new String[]{pythonExe, pythonPath};
            long startTime = System.currentTimeMillis();
            String type = args[args.length - 1];
            Process process = Runtime.getRuntime().exec(args);

            new Thread(new OutputHandlerRunnable(process.getInputStream(), type, "info")).start();
            new Thread(new OutputHandlerRunnable(process.getErrorStream(), type, error)).start();

            // 返回值为0 - 表示调用python脚本成功;
            // 返回值为1 - 表示调用python脚本失败。
            int re = process.waitFor();
            System.out.println("调用 python 脚本参数:" + Arrays.toString(args));
            System.out.println("调用 python 脚本耗时:" + (System.currentTimeMillis() - startTime) / 1000);
            System.out.println("调用 python 脚本是否成功:" + ((re == 0) ? "成功" : "失败"));
        } catch (Exception e) {
            System.err.println(ExceptionUtil.stacktraceToString(e));
        }
    }

    private static class OutputHandlerRunnable implements Runnable {
        private InputStream in;
        private String type;
        private String infoLevel;


        @Override
        public void run() {
            StringBuffer result = new StringBuffer();
            try (BufferedReader bufr = new BufferedReader(new InputStreamReader(this.in, "GBK"))) {
                String line = null;
                while ((line = bufr.readLine()) != null) {
                    if (ObjectUtil.contains(line, "save data:")) {
                        String pyResult = line.replace(" save data:", "").replace("save data:", "");
                        if (!ObjectUtil.isEmpty(pyResult)) {
                            pyResultMap.put(type, JSONUtil.parseArray(pyResult));
                        }
                    } else {
                        result.append(line).append("\n\r");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (ObjectUtil.equal(infoLevel, error)) {
                System.out.println("调用 python 脚本返回结果:" + result);
            }
        }

        public InputStream getIn() {
            return in;
        }

        public void setIn(InputStream in) {
            this.in = in;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getInfoLevel() {
            return infoLevel;
        }

        public void setInfoLevel(String infoLevel) {
            this.infoLevel = infoLevel;
        }

        public OutputHandlerRunnable(InputStream in, String type, String infoLevel) {
            this.in = in;
            this.type = type;
            this.infoLevel = infoLevel;
        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值