Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
w_hat
Jul 8, 2003
I need some help with a small problem I'm having. I wrote my first python program ever to send cpu usage to an arduino for display on a vintage panel meter, just for fun. Occasionally it will quit with the error:

C:\Python25>cpumon.py
Traceback (most recent call last):
File "C:\Python25\cpumon.py", line 35, in <module>
idleTime = win32pdh.GetFormattedCounterValue(ProcCounter,format)[1]
pywintypes.error: (-2147481642, 'GetFormattedCounterValue', 'No error message is
available')

Why and how can I simply ignore this and keep going? I know that's typically not a good idea but in this case it simply doesn't matter if it misses an update or something.

Here's the code:
code:
import win32pdh
import serial 
from time import sleep

PdhQuery = win32pdh.OpenQuery(None, 0)
ProcPath = win32pdh.MakeCounterPath((None, "Processor", "_Total", None, 0, "% Processor Time"))
ProcCounter = win32pdh.AddCounter(PdhQuery, ProcPath, 0)
win32pdh.CollectQueryData(PdhQuery)

ser = serial.Serial(2)

def setpriority(pid=None,priority=1):
    """ Set The Priority of a Windows Process.  Priority is a value between 0-5 where
        2 is normal priority.  Default sets the priority of the current
        python process but can take any valid process ID. """
        
    import win32api,win32process,win32con
    
    priorityclasses = [win32process.IDLE_PRIORITY_CLASS,
                       win32process.BELOW_NORMAL_PRIORITY_CLASS,
                       win32process.NORMAL_PRIORITY_CLASS,
                       win32process.ABOVE_NORMAL_PRIORITY_CLASS,
                       win32process.HIGH_PRIORITY_CLASS,
                       win32process.REALTIME_PRIORITY_CLASS]
    if pid == None:
        pid = win32api.GetCurrentProcessId()
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    win32process.SetPriorityClass(handle, priorityclasses[priority])

setpriority(None,3)

while 1:
	win32pdh.CollectQueryData(PdhQuery)
	format = win32pdh.PDH_FMT_LONG | win32pdh.PDH_FMT_NOSCALE
	idleTime = win32pdh.GetFormattedCounterValue(ProcCounter,format)[1]
	load = idleTime
	#print str(idleTime)
	if load == 100:
		load = 99
	elif load < 10:
		ser.write('0'+str(load))
	else:
    		ser.write(str(load))
    	sleep(.4)

Adbot
ADBOT LOVES YOU

w_hat
Jul 8, 2003

JoeNotCharles posted:


Just using "except:" will ignore ALL errors, but that's not a good idea since you should at least find out what kinds of errors can occur before deciding to ignore them.

File "C:\Python25\cpumon.py", line 44
except pywintypes.error:
^
SyntaxError: invalid syntax

Do I need to import something?

Edit: Just kidding, I found it, thanks for the help.

w_hat fucked around with this message at 00:27 on Feb 2, 2008

w_hat
Jul 8, 2003

JoeNotCharles posted:

Nope, should be fine. (Don't actually put the "..." in - I just used that to mark code that I'd cut out.) Are you sure your indentation's correct?

You might have to import pywintypes, but it would give a different error if that was the problem.

From one error to another:
Traceback (most recent call last):
File "C:\Python25\cpumon.py", line 44, in <module>
except pywintypes.error:
NameError: name 'pywintypes' is not defined

I should probably get around to actually learning Python instead of hacking it together.

w_hat
Jul 8, 2003

JoeNotCharles posted:

Really, what was unclear about that?

Christ, I totally missed that, thanks.

  • Locked thread