Windows Services

Larry Bates larry.bates at websafe.com
Tue Apr 4 09:47:56 EDT 2006


rodmc wrote:
> Hello,
> 
> I am trying to figure out how to write a windows service, I am totally
> new to it. I have written a simple test case, however it doesn't appear
> to do anything. Although it installs and appears to start, when I run a
> query of its status (using another app) it says it is not running.  As
> a note I am running it as the administrator in a Win2k machine to avoid
> any problems with permissions - although I would rather not do this for
> ever :-)
> 
> I install the software as follows:
> 
> python testservice.py install
> python testservice.py start
> 
> I am sure I am missing something out as I am basically working from
> code samples for other solutions which I have seen. In particular one
> for getting CherryPy to run as a service.
> 
> When I have figured this all out I intend to add in some code I have
> already written which runs a server application.
> 
> It may be all rather obvious but I am quite new Python (just a few
> months) and totally new to Windows services.
> 
> Thanks in advance for any help.
> 
> Best,
> 
> Rod
> 
> 
> import win32serviceutil
> import win32service
> import win32event
> import os,sys
> 
> class helloworld:
> 
>     def test(self):
>         x=1
>         filename=open("c:test.txt","w")
>         while x<100000:
>             print >>filename,x
>             x=x+1
> 
> class MyService(win32serviceutil.ServiceFramework):
>     """NT Service."""
> 
>     _svc_name_ = "Test"
>     _svc_display_name_ = "Test"
> 
>     def __init__(self, args):
>         win32serviceutil.ServiceFramework.__init__(self, args)
>         # create an event that SvcDoRun can wait on and SvcStop
>         # can set.
>         self.stop_event = win32event.CreateEvent(None, 0, 0, None)
> 
>     def SvcDoRun(self):
>         print "got here"
>         helloworld.test()
>         win32event.WaitForSingleObject(self.stop_event,
> win32event.INFINITE)
> 
>     def SvcStop(self):
>         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>         win32event.SetEvent(self.stop_event)
> 
> if __name__ == '__main__':
>     win32serviceutil.HandleCommandL
> 
I found Mark Hammond and Andy Robinson's book Python Programming on
Win32 to be a great resource when I wrote my first service.  It has
several examples that helped a lot.

Remember services run inside of infinite loops.  Inside SvcDoRun
you must have some construct like:

    while 1:
        #-----------------------------------------------------------------
        # Wait for service stop signal, if I timeout, loop again
        #-----------------------------------------------------------------
        rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
        #
        # Check to see if self.hWaitStop happened
        #
        if rc == win32event.WAIT_OBJECT_0:
            #
            # Stop signal encountered
            #
            break

        else:
            #
            # Put body of service here
            #



You also should probably change win32event.INFINITE to some number (expressed
in milliseconds) or your service gets blocked and doesn't do anything.

The sequence goes like this:

Inside an infinite loop, sleep for some time
Wake up and check if you received a stop signal
If you have, exit the infinite loop (by returning from SvcDoRun)
If not, run whatever code you want to execute each time you loop

Hope this helps.
Larry Bates



More information about the Python-list mailing list