I created a service based on this code Is it possible to run a Python script as a service in Windows? If possible, how?
The problem is that I cant stop the service, smoothly.
To stop the service I would need to recheck a flag for a stop event every period of time.
Is there any better way to stop a service in python?
This is how I currently try to stop my service.
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):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
myStatusThread = threading.Thread(target=win32event.WaitForSingleObject, args=(self.hWaitStop, win32event.INFINITE))
myStatusThread.start()
while True:
if not myStatusThread.isAlive():
sys.exit(0)
else:
doStuff()#do something that takes some time
So the service only stops once "doStuff" is done, which could take a long time.
I would like to stop the service as soon as the user stops it in the services.