Creating and managing a Windows service (part 1)

I'm starting a longer piece on creating and managing a python based Windows service, so look for the other parts in this blog for the complete "recipe"

First, our tools: py2exe has, among its deployment targets, a "windows service" option, so we'll need that. To manage the service and interact with the Windows event log, the Python win32 extension is needed.

Py2exe has a sample service in the samples/advanced folder, on which I've based my code.

import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import time, sys, StringIO

class MyService(win32serviceutil.ServiceFramework):
    _svc_name_ = "MyService"
    _svc_display_name_ = "My pretty pretty service"
    _svc_deps_ = ["EventLog"]
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # Write a 'started' event to the event log...
        win32evtlogutil.ReportEvent(self._svc_name_,
                                    servicemanager.PYS_SERVICE_STARTED,
                                    0, # category
                                    servicemanager.EVENTLOG_INFORMATION_TYPE,
                                    (self._svc_name_, ''))

        import servicemanager #another way to write to the event log
        servicemanager.LogInfoMsg("Started the service")    #seems to fix the msgids that are logged into the event log
              
        while True:
            # wait for beeing stopped...
            rc = win32event.WaitForSingleObject(self.hWaitStop, 1)
            if rc==win32event.WAIT_OBJECT_0:
                break
            time.sleep(2)
            servicemanager.LogInfoMsg('next loop')

        # and write a 'stopped' event to the event log.
        win32evtlogutil.ReportEvent(self._svc_name_,
                                    servicemanager.PYS_SERVICE_STOPPED,
                                    0, # category
                                    servicemanager.EVENTLOG_INFORMATION_TYPE,
                                    (self._svc_name_, ''))

if __name__ == '__main__':
    # Note that this code will not be run in the 'frozen' exe-file!!!
    win32serviceutil.HandleCommandLine(MyService)

Next, how to create a Windows service from python.

Comments