Skip to content

Instantly share code, notes, and snippets.

@aconz2
Created October 19, 2017 17:17
Show Gist options
  • Save aconz2/22a22af34287564cb3d2722592871557 to your computer and use it in GitHub Desktop.
Save aconz2/22a22af34287564cb3d2722592871557 to your computer and use it in GitHub Desktop.
with block for having a service stopped and then started on exit
import win32serviceutil
class StopStartService:
STARTED, STOPPED = 4, 1
def __init__(self, service_name, timeout=5):
self.service_name = service_name
self.timeout = timeout
def __enter__(self):
self._enter_if_not(win32serviceutil.StopService, StopStartService.STOPPED)
def __exit__(self, exc_type, exc_val, exc_tb):
self._enter_if_not(win32serviceutil.StartService, StopStartService.STARTED)
def _enter_if_not(self, f, status):
if win32serviceutil.QueryServiceStatus(self.service_name)[1] != status:
f(self.service_name)
win32serviceutil.WaitForServiceStatus(storis_service_name, status, self.timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment