win32print how to lower cpu load?

G

Guy Robinson

Hello,

I've got this working print queue monitor and currently it checks the
queues every 1 second. I'm worried that I might miss events if I set
the delay to more than this. Particularly as I would like to perform
functions depending on status codes. When I don't have a delay set, the
CPU usage is huge 70%.

My question is, is there an 'intelligent' way of monitoring the spooler?
I guess what I'm thinking is some low cpu method of watching for
activity on the spooler, and triggering my program as the spooler stops
and starts.

Suggestions?

Regards,

Guy

Here's my code so far:

try:
import win32print
except:
print 'win32all required .'

import time

#constants

wp = win32print

# lists and dictionaries of constants
ptypelist =
[(wp.PRINTER_ENUM_SHARED,'shared'),(wp.PRINTER_ENUM_LOCAL,'local'),(wp.PRINTER_ENUM_CONNECTIONS,'network')]
cmds = {'Pause':wp.JOB_CONTROL_PAUSE, 'cancel':wp.JOB_CONTROL_CANCEL,
'resume':wp.JOB_CONTROL_RESUME,
'prior_low':wp.MIN_PRIORITY,'prior_high':wp.MAX_PRIORITY,'prior_normal':wp.DEF_PRIORITY
}
status codes =
{'deleting':wp.JOB_STATUS_DELETING,'error':wp.JOB_STATUS_ERROR,'offline':wp.JOB_STATUS_OFFLINE,'paper
out':wp.JOB_STATUS_PAPEROUT,'paused':wp.JOB_STATUS_PAUSED,'printed':wp.JOB_STATUS_PRINTED,'printing':wp.JOB_STATUS_PRINTING,'spooling':wp.JOB_STATUS_SPOOLING}

class PMFuncs:

def __init__(self):

# initialise the list of printers
self.PList =[]

def PrinterList(self):

# returns a list of dicts.
# this gets the default printer
tmpdic ={}
DefPName = win32print.GetDefaultPrinter()

# Get the default printer firstso we can add this to the list
of printers

for pt in ptypelist:

try:

for (Flags,pDescription,pName,pComment) in
list(win32print.EnumPrinters(pt[0],None,1)):
tmpdic ={}
tmpdic['PType'] = pt[1]
tmpdic['Flags'] = Flags
tmpdic['Description'] = pDescription
#test for if this is the default printer
if pName == DefPName:
tmpdic['DefPrinter':True]
else:
tmpdic['DefPrinter':False]
tmpdic['Name'] = pName
tmpdic['Comment'] = pComment
self.PList.append(tmpdic)
except:
pass #no printers of this type so don't add anything

return self.PList #list of installed printers

def GetJobList(self,printer):

phandle = win32print.OpenPrinter(printer)
#now get all the print jobs (start at job 0 and -1 for all jobs)
jlist = win32print.EnumJobs(phandle,0,-1,1)
win32print.ClosePrinter(phandle)
return jlist # this lists all jobs on all printers

def GetJobInfo(self,printer,jobID):

phandle = win32print.OpenPrinter(printer)
ilist = win32print.GetJob(phandle,jobID,1)
win32print.ClosePrinter(phandle)
return ilist #this lists all info available at level 1 for
selected job.

def SetJobCmd(self, printer, jobID , JobInfo , RCmd ):

phandle = win32print.OpenPrinter(printer)
win32print.SetJob(phandle,jobID,1,JobInfo,Cmds[RCmd])
win32print.ClosePrinter(phandle)

# test functions
e = PMFuncs()
while 1:

time.sleep(1)
e = PMFuncs()
for i in e.PrinterList():

try:
p = e.GetJobList(i['Name'])
for w in p:
print e.GetJobInfo(i['Name'],w['JobID'])

except:
pass
 
E

Emile van Sebille

Guy Robinson:
I've got this working print queue monitor and currently it checks the
queues every 1 second. I'm worried that I might miss events if I set
the delay to more than this. Particularly as I would like to perform
functions depending on status codes.

You could pause all the printers, live with the one second delay, and
selectively release jobs for printing and deletion.

Alternately, missing entire jobs seems to indicate that everything is
working as it should. Aren't the most interesting status' the 'things
aren't normal' ones?

When I last needed to monitor printers, I monitored the spool queue on
disk instead, but I was interested in capturing the spooled content
and not the status.

HTH,
 
M

Mark Hammond

Emile said:
Guy Robinson:



You could pause all the printers, live with the one second delay, and
selectively release jobs for printing and deletion.

Or maybe arrange for all jobs to be placed in the queue in a "paused"
state, and have your monitor release the jobs?

Either way, for the OP, the general solution to this kind of question is
to find the best solution in *any* language. Most often, you will find
sample code in either VB or C++ - often from MS - but it can generally
be ported to Python fairly easily once you understand exactly what you
are trying to do.

Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top