首页 > 其他分享 >flask获取硬件资源信息

flask获取硬件资源信息

时间:2022-10-02 10:56:21浏览次数:56  
标签:__ 硬件资源 flask app python 获取 pynvml gpu --

1.获取gpu信息
[root@ks-devops python]# cat gpu_info.py 
import pynvml
UNIT = 1024 * 1024


def GpuInfo():
    pynvml.nvmlInit() #初始化
    gpuDeriveInfo = pynvml.nvmlSystemGetDriverVersion()
    print("Drive: ", str(gpuDeriveInfo, encoding='utf-8')) #显示驱动信息


    gpuDeviceCount = pynvml.nvmlDeviceGetCount()#获取Nvidia GPU块数
    print("GPU", gpuDeviceCount )

    gpu_list = []
    for i in range(gpuDeviceCount):

        handle = pynvml.nvmlDeviceGetHandleByIndex(i)#获取GPU i的handle,后续通过handle来处理
        gpuName = str(pynvml.nvmlDeviceGetName(handle), encoding='utf-8')

        memoryInfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
        print("GPU", i, ":", gpuName, memoryInfo.total/UNIT,"MB")
        gpu_list.append(str(i)+":"+gpuName+" "+str(memoryInfo.total/UNIT)+"MB")
    return gpu_list



if __name__ == "__main__":
    gpu_info = GpuInfo()
    pynvml.nvmlShutdown()
    #print(gpu_devices)

 

2.硬件序列号设定(自定义的,随机大小写字母+时间)
[root@ks-devops python]# cat sn_code.py 
import string
import random
import time
def sncode():
    # Randomly choose a letter from all the ascii_letters
    str = ""
    for num in range(9):
        randomLetter = random.choice(string.ascii_letters)
        str += randomLetter
        #print(randomLetter,end="")
        
    now =  time.localtime()
    now_time = time.strftime("%Y%m%d%H%M%S", now)
    #print(now_time)
    print(str+now_time)
    return str+now_time
#sncode()

if __name__ == '__main__':
    #sncode()
    pass

 

3.flask框架接口定义
[root@ks-devops python]# cat app.py 
from flask import Flask,jsonify
from gpu_info import GpuInfo
from sn_code import sncode
app = Flask(__name__)

@app.route('/gpuinfo')
def gpu_info():
    gi = jsonify("gpuinfo",GpuInfo())
    return gi

@app.route('/sncode')
def sn_code():
    sn = jsonify("sn",sncode())
    return sn

@app.route('/health')
def status():
    return "ok"

@app.route('/')
def index():
    return "test lipc"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
    

 

4.制作python底层镜像
(1)下载centos:7基础镜像
docker pull centos:7
(2)运行临时容器
docker run -it --rm --name python-test centos:7
(3)安装python环境和flask库
yum -y install python3
python3 -m pip install --upgrade --force pip
pip3 install sanic
pip3 install flask
pip3 install pynvml
yum clean all
(4)将容器打包成镜像
#查看容器id
docker ps |grep python-test   
#将容器打包成镜像
docker commit f185eb722b13  172.16.4.17:8888/base/centos-py3:v1

5.制作python运行镜像
#当前目录下的文件
[root@ks-devops python]# ls
app.py    Dockerfile  gpu_info.py   py_base_images.sh  sn_code.py  start.sh  

(1)编写dockerfile
[root@ks-devops python]# cat Dockerfile 
FROM 172.16.4.17:8090/base/centos-py3:v2
WORKDIR /data
RUN virtualenv venv
RUN source ./venv/bin/activate
COPY . /data/venv
HEALTHCHECK --interval=5s --timeout=3s  --retries=3 \
    CMD curl -fs http://127.0.0.1:5000/health || exit 1
ENTRYPOINT ["sh","/data/venv/start.sh"] 

(2)制作镜像
docker build . -t 172.16.4.17:8888/test/up-lipc:v1.1

(3)运行容器
docker run -itd --name flask-test --network host --gpus all  172.16.4.17:8888/test/up-lipc:v1.1

(4)查看容器运行状态
docker ps

 

6.访问测试

 

 

 

标签:__,硬件资源,flask,app,python,获取,pynvml,gpu,--
From: https://www.cnblogs.com/Leonardo-li/p/16748394.html

相关文章