如果语句始终运行,即使不是真的

时间:2019-04-25 07:20:24

标签: python power-management

我想在系统使用400瓦以上功率时设置功率上限。我尝试使用简单的if语句来执行此操作,但是当当前功率为256 W时,也会运行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

当ist在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

或者您对我的建议还有其他建议吗?

1 个答案:

答案 0 :(得分:2)

将值转换为浮点数。

例如:

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")