Windows Service written in python, not detecting shutdown events and stopping gracefully Windows Service written in python, not detecting shutdown events and stopping gracefully python python

Windows Service written in python, not detecting shutdown events and stopping gracefully


Did you try the PRESHUTDOWN? Usually SHUTDOWN is too late.

def GetAcceptedControls(self):    # Accept SESSION_CHANGE control    rc = win32serviceutil.ServiceFramework.GetAcceptedControls(self)    rc |= win32service.SERVICE_ACCEPT_SESSIONCHANGE    rc |= win32service.SERVICE_ACCEPT_SHUTDOWN    rc |= win32service.SERVICE_ACCEPT_PRESHUTDOWN    return r

Then in SvcOtherEx() intercept the Pre Shutdown event and ask for more time.

if win32service.SERVICE_CONTROL_PRESHUTDOWN:    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, waitHint=10000)    win32event.SetEvent(self.stop_event)