python - 即使不为 true,If 语句也始终运行

标签 python power-management

我想在系统使用超过 400 瓦时设置功率上限。我尝试使用简单的 if 语句来做到这一点,但当当前功率为 256 瓦时也会运行 if 语句

我的代码如下

a=400

node1 = Node(0, 0, 0, 0)
node1.getPowerUsage()
node1.getGpuPowerUsage()
node1.getCpuPowerUsage()
node1.setPowercap()


if(node1.getCpuPowerUsage() > a):
    node1.setPowercap()

else:
    print("nothing to do")

node1.getCpuPowerUsage() 的输出看起来像这样

print node1.getCpuPowerUsage()
277.54

当功率低于 400 瓦时,我希望看到这样的输出 nothing to do 但实际上我得到这个输出 Current Power : 247 Watts

Enabled persistence mode for GPU 00000000:35:00.0.
Enabled persistence mode for GPU 00000000:5B:00.0.
Enabled persistence mode for GPU 00000000:CD:00.0.
Enabled persistence mode for GPU 00000000:AA:00.0.
All done.
Power limit for GPU 00000000:35:00.0 was set to 125.00 W from 250.00 W.
Power limit for GPU 00000000:5B:00.0 was set to 125.00 W from 250.00 W.
Power limit for GPU 00000000:CD:00.0 was set to 125.00 W from 250.00 W.
Power limit for GPU 00000000:AA:00.0 was set to 125.00 W from 250.00 W.
All done.
nothing to do

或者您对我如何做到这一点还有其他建议吗?

最佳答案

将值转换为 float 。

例如:

a=400

node1 = Node(0, 0, 0, 0)
node1.getPowerUsage()
node1.getGpuPowerUsage()
node1.getCpuPowerUsage()
node1.setPowercap()


if(float(node1.getCpuPowerUsage()) > a):
    node1.setPowercap()
else:
    print("nothing to do")

关于python - 即使不为 true,If 语句也始终运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843862/

相关文章:

python - 我的摄像头实时直方图

android - 我的应用程序可以控制哪些设置来节省电池生命周期?

objective-c - 如何以编程方式使用 Cocoa 获取当前电池电量

batch-file - 创建了一个脚本来切换盖子关闭操作( sleep /不执行任何操作)我可以看到它生效,但它不起作用

iOS : Region monitoring and battery usage

python - 我收到有关日期时间的错误消息

python - 为什么导入的包在其他包中可见?

c++ - 让 QT 响应电源事件( sleep /休眠)

python - 弹出程序执行-python

python - Haskell 与 Python 线程模型