'Wait until the service can be stopped.'

def StopPreviousService(self):

    service = config.CONFIG['Nanny.service_name']
    try:
        status = win32serviceutil.QueryServiceStatus(service)[1]
    except pywintypes.error as e:
        logging.info('Unable    to    query    status    of    service:    %s    with    error:    %s', service, e)
        return
    for _ in range(20):
        if (status == win32service.SERVICE_STOPPED):
            break
        elif (status != win32service.SERVICE_STOP_PENDING):
            logging.info('Attempting    to    stop    service    %s', service)
            try:
                win32serviceutil.StopService(service)
            except pywintypes.error as e:
                logging.info('Unable    to    stop    service:    %s    with    error:    %s', service, e)
        time.sleep(1)
        status = win32serviceutil.QueryServiceStatus(service)[1]
    if (status != win32service.SERVICE_STOPPED):
        service_binary = config.CONFIG['Nanny.service_binary_name']
        output = subprocess.check_output(['taskkill', '/im', ('%s*' % service_binary), '/f'], shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        logging.debug('%s', output)
        time.sleep(2)
