[python-win32] Pythonservice.exe running at 100%

David Rock David Rock <david@rock.homelinux.com>
Tue, 19 Mar 2002 10:45:50 -0600


On Tue, Mar 19, 2002 at 05:13:30PM +1100, Mark Hammond wrote:
> > Every time I try to run a service on Windows NT4, it always runs at 100%
> > CPU utilization. Why does pythonservice.exe do this and is there any way
> > that I can throttle it back? I have managed to minimize the amount of
> > time that the service app I am running takes, so that it is not a
> > tremendous strain on the system (just a couple seconds) but I would like
> > to manage this better.
> 
> Something is almost certainly wrong with your service.  The test services do
> not exhibit this.  Check that your loops that are supposed to be waiting for
> something actually are :)

Here is the core of my SvcDoRun:

        timeout = 120000
        waitHandle = self.hWaitStop

        while 1:
            rc = win32event.WaitForSingleObject( waitHandle, timeout )
            if rc == win32event.WAIT_OBJECT_0:
                #stop event
                break
            else:
                start_time = time.time()
                self.run_test(loop)
                end_time   = time.time()
                diff_time  = end_time - start_time
                self.send_results( start_time, end_time, diff_time,
                                   string.lower(t_server), c_server,
                                   ftpuser, ftppass, patrol )


While it is waiting for the self.hWaitStop there is no activity, which
is what I expect. When the timeout hits and runs the else statement,
this is where I have problems. What I do is run a test loop to measure
the amount of time it takes for a system to do some basic I/O operations
and then it ftps those results to a collection server. Here are the two
modules I am using for this:

    def run_test(self, loop):

       # Create tempfile to write data to
       tmp = tempfile.mktemp('.tmp')
       fp = open( tmp, 'w' )

       for x in xrange(1,loop):
          print >>fp, 'This is a file write test:'
          print >>fp, '%s' % ( os.listdir( os.environ['systemdrive'] ) )
          fp.seek(0)
          fp.truncate()

       fp.close()
       os.remove( tmp )

    def send_results(self,stime,etime,dtime,tserver,cserver,
                     ftpuser,ftppass,patrol):
       if dtime > 3:
          err = 'ERROR'
       else:
          err = ''

       if patrol == 'no':
          tmp = tempfile.mktemp('.tmp')
          fp = open( tmp, 'w' )
          tmpdate = time.asctime(time.localtime(stime))
          print >>fp, '%s,%s,%f,%s' % ( tserver, tmpdate, dtime, err )
          fp.close()

          # ftp to logging server and append to a log file.
          ftpcmd = 'APPE pyperf/' + tserver + '.log'
          fp = open( tmp, 'r' )
          ftp = ftplib.FTP( cserver )
          ftp.login( ftpuser, ftppass )
          ftp.storlines( ftpcmd, fp )
          ftp.quit()
          fp.close()
          os.remove( tmp )

       print '%d' % ( dtime )

Somewhere within one of these two modules is where it pegs the system at
100%. I am pretty new to using Python for writing a service or any
Windows event driven programs for that matter, so I have no idea if
there is a better way to do this or not. 

You mentioned that I should be making sure that the loop is actually
waiting for something.  The only loop that waits for anything is the
main loop that waits for the stop service event. Everything else is
running after that point. Is there anything I can do within the other
modules to get it under control?

Thanks.

-- 
David Rock
david@rock.homelinux.com