Jump to content

Different Output

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Shadow21

Shadow21

    Newbie

  • Members
  • PipPip
  • 12 posts
I just started really learning Python a couple of days ago and I'm trying to make my first extremely simple useful program. All it is is a variable that is assigned an integer for the temperature of my GPU and it needs to choose the correct fan speed depending on the temperature.

The script:
#!/usr/bin/python3


import os


temp = os.system("nvidia-settings -q gpucoretemp -t")


if temp <= 40:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=30")

elif temp >= 41 and temp <= 50:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=35")

elif temp >= 51 and temp <= 55:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=40")

elif temp >= 56 and temp <= 60:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=50")

elif temp >= 61 and temp <= 70:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=65")

elif temp >= 71 and temp <= 80:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=75")

elif temp >= 81 and temp <= 90:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=90")

else:

       os.system("nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=100")


When I run "nvidia-settings -q gpucoretemp -t" by itself in the Linux terminal it outputs a temperature (Ex. 53) but when I use the command in my script and assign it to a variable it would saves as:

53

0

The 0 is throwing off my script and making the fan change to 30% instead of 40%.

Why is it putting a 0 in the output when I use the command in my Python script and how do I make it only save the 53 (or other temperature)?

#2
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts
help(subprocess)
exact

>>> import subprocess

>>> output = subprocess.Popen(['cat', '/etc/fstab'], stdout=subprocess.PIPE).communicate()[0]

>>> output

b'# Entry for /dev/sda1 :\nUUID=a6071da9-94d7-4b46-80ba-be9279375353 / ext3 acl,relatime 1 1\nnone /proc proc defaults 0 0\n# Entry for /dev/sda6 :\nUUID=c9a84d1a-c49e-4994-b71f-23c601b26130 swap swap defaults 0 0\n\n# My entry\n#/dev/fd0 /mnt/floppy vfat users,umask=000 0 0\n'

>>>


simple

>>> import subprocess

>>> s = subprocess.getoutput('echo x')

>>> s

'x'

>>> s = subprocess.getoutput('cat /etc/fstab')

>>> s

'# Entry for /dev/sda1 :\nUUID=a6071da9-94d7-4b46-80ba-be9279375353 / ext3 acl,relatime 1 1\nnone /proc proc defaults 0 0\n# Entry for /dev/sda6 :\nUUID=c9a84d1a-c49e-4994-b71f-23c601b26130 swap swap defaults 0 0\n\n# My entry\n#/dev/fd0 /mnt/floppy vfat users,umask=000 0 0'

>>> 



#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I don't have an NVidia card on my linux machine so I'm not really in a position to test an NVidia settings tool. However, I know how os.system works, it's identical to the C call system(), so it's most likely that nvidia_settings is returning 0 as it's exit status (success), and just printed the value 53 to wherever stdout was for that process. You want to use subprocess.getoutput().
#!/usr/bin/python3


import os, subprocess


temp = int(subprocess.getoutput("nvidia-settings -q gpucoretemp -t"))
WARNING: Code above is not safe! It attempts to read to int the raw output of nvidia-settings, which could be non-numeric.

See if that does any better. Also consider a loop like this instead of all those elif statements:
for check, speed in zip([40, 50, 55, 60, 70, 80, 90, 1000000000], [30, 35, 40, 50, 65, 75, 90, 100]):

    if (temp <= check):

        os.system("nvidia-settings -a [fan:0:]/GPUCurrentFanSpeed=" + str(speed))

That should work for any temperature under a billion degrees C. :P
Wow I changed my sig!

#4
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts

def get_cmd(speed):

    return "nvidia-settings -a [fan:0]/GPUCurrentFanSpeed=" + str(speed)

yes ?

#5
Shadow21

Shadow21

    Newbie

  • Members
  • PipPip
  • 12 posts

ZekeDragon said:

I don't have an NVidia card on my linux machine so I'm not really in a position to test an NVidia settings tool. However, I know how os.system works, it's identical to the C call system(), so it's most likely that nvidia_settings is returning 0 as it's exit status (success), and just printed the value 53 to wherever stdout was for that process. You want to use subprocess.getoutput().
#!/usr/bin/python3


import os, subprocess


temp = int(subprocess.getoutput("nvidia-settings -q gpucoretemp -t"))

That worked great and now my script is working like it should. Like I said, I just started learning Python so I'm not familiar with many modules yet.

Thanks for the help!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users