小技巧随手记:Python查看windows下GPU的使用情况

简介: 小技巧随手记:Python查看windows下GPU的使用情况

 目录

一、使用nvidia-smi查看Windows的CUDA版本及GPU信息

二、使用pynvml查看GPU使用情况的命令

三、python 中使用GPUti实时查看GPU状况

四、使用gpustat库实时监测GPU使用情况(Linux下可以,Windows下不行的,衍生问题暂时没有一个好的解决方案)


一、使用nvidia-smi查看Windows的CUDA版本及GPU信息

在cmd中输入如下命令:

nvidia-smi

image.gif

image.gif

二、使用pynvml查看GPU使用情况的命令

首先安装nvidia-ml-py包:

image.gif

代码及对应解释如下:

import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0) # 指定显卡号
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
print(meminfo.total/1024**2) #总的显存大小(float)
print(meminfo.used/1024**2)  #已用显存大小(float)
print(meminfo.free/1024**2)  #剩余显存大小(float)
print(pynvml.nvmlDeviceGetCount())#显示有几块GPU

image.gif

image.gif

三、python 中使用GPUti实时查看GPU状况

首先pip安装 gputi包:

image.gif

运行如下命令:

import GPUtil
GPUtil.showUtilization()

image.gif

image.gif

四、使用gpustat库实时监测GPU使用情况(Linux下可以,Windows下不行的,衍生问题暂时没有一个好的解决方案)

Linux下:

image.gif

Windows下(失败):

首先安装gpustat包:

image.gif

在cmd中输入如下命令:

gpustat --w

image.gif

image.gif

报错:

ModuleNotFoundError: No module named '_curses'

image.gif

image.gif

其实好像是curses库不支持Windows。

这个问题可以解决,先使用where python命令找到自己python的安装路径:

image.gif

再运行如下代码查看自己python的版本:

import platform
print(platform.python_version())

image.gif

image.gif

在如下链接下载对应版本的curses包:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

下载你自己Python对应的版本!cp39表示py3.9   64表示64位

image.gif

把whl文件放入Script文件夹后,进入Script文件夹:

image.gif

image.gif

image.gif

重新在cmd中输入:

gpustat --w

image.gif

又报错:

ModuleNotFoundError: No module named 'fcntl'

image.gif

image.gif

这个报错也可以解决:

在 python 安装目录 中 Lib目录( 比如:D:\Python39\Lib ),创建 fcntl.py ,内容如下:

def fcntl(fd, op, arg=0):
    return 0
def ioctl(fd, op, arg=0, mutable_flag=True):
    if mutable_flag:
        return 0
    else:
        return ""
def flock(fd, op):
    return
def lockf(fd, operation, length=0, start=0, whence=0):
    return

image.gif

image.gif

 

当然到这里你就没问题了是最好的,如果还报错:

ModuleNotFoundError: No module named ‘termios’

image.gif

那你就凉凉了~~


这个东西是linux下的 windows没有,所以你不用查了,我翻了好多博客都没有解决方案,唯一的一个解答是(变通方法就超级麻烦了,这里仅提供一个学习链接):

Airflow | 脚本东零西散?Airflow 快速搭建 pipeline(超详细)_HinGwenWoong的博客-CSDN博客

image.gif

相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
相关文章
|
9天前
|
计算机视觉 Windows Python
windows下使用python + opencv读取含有中文路径的图片 和 把图片数据保存到含有中文的路径下
在Windows系统中,直接使用`cv2.imread()`和`cv2.imwrite()`处理含中文路径的图像文件时会遇到问题。读取时会返回空数据,保存时则无法正确保存至目标目录。为解决这些问题,可以使用`cv2.imdecode()`结合`np.fromfile()`来读取图像,并使用`cv2.imencode()`结合`tofile()`方法来保存图像至含中文的路径。这种方法有效避免了路径编码问题,确保图像处理流程顺畅进行。
75 1
|
29天前
|
并行计算 TensorFlow 算法框架/工具
Windows11+CUDA12.0+RTX4090如何配置安装Tensorflow2-GPU环境?
本文介绍了如何在Windows 11操作系统上,配合CUDA 12.0和RTX4090显卡,通过创建conda环境、安装特定版本的CUDA、cuDNN和TensorFlow 2.10来配置TensorFlow GPU环境,并提供了解决可能遇到的cudnn库文件找不到错误的具体步骤。
127 3
|
29天前
|
存储 数据可视化 Python
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
【python】python tkinter 计算器GUI版本(模仿windows计算器 源码)【独一无二】
|
10天前
|
索引 Python
干货!20个Python使用小技巧
干货!20个Python使用小技巧
|
14天前
|
Python Windows
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
|
14天前
|
Python Windows 内存技术
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
|
15天前
|
JavaScript Java Python
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
|
15天前
|
Linux Python Windows
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
|
2月前
|
Python
Python小技巧:一种字符串的排序方式
Python小技巧:一种字符串的排序方式
17 0
|
1月前
|
Linux 开发者 Python
从Windows到Linux,Python系统调用如何让代码飞翔🚀
【8月更文挑战第4天】在编程领域,跨越操作系统障碍是常见挑战。Python以“编写一次,到处运行”的理念简化了这一过程。通过其标准库如os、subprocess等,Python提供了跨平台的系统操作工具,确保开发者无需针对不同系统编写特定代码。例如,`open`函数在Windows和Linux上具有一致性;而`subprocess`模块则简化了跨平台执行系统命令的过程。此外,第三方库如psutil进一步增强了Python的跨平台能力。总之,Python强大的系统调用能力让开发者能轻松地编写高效且可移植的代码。
28 0
下一篇
DDNS