Win2000 service and pywintypes.api_error

Cesare Zamatti nospam at nospam.it
Fri Dec 13 23:07:08 EST 2002


Hey all,

I 've written an  NTservice for a Win2000 Server using
win32serviceutil.ServiceFramework
with  python 2.2.2 (from python.org)  and win32all-148 (from
starship.python.net)
I 've written it with some samples found on the net.
The service starts and stops without problems, but if I look at the
eventViewer, when I stop the service, it reports this error (in the
application log):

PythonService:
The instance's SvcRun() method failed File "E:\Python22\lib\site-
packages\win32\lib\win32serviceutil.py",
 line 639, in SvcRun
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
File "E:\Python22\lib\site-packages\win32\lib\win32serviceutil.py",
line 610, in ReportServiceStatus win32service.SetServiceStatus( self.ssh,
status)
pywintypes.api_error: (6, 'SetServiceStatus', 'The handle is invalid.')

I' m not able to find out the reason why this  error appears in the
eventViewer log.
All this is giving  me a great  headhache  :(

I report you a sample code (the shortest possible ).
If someone has some idea many thanks in advance ........
Cesare

# ==========================================================================
#  SAMPLE SERVICE
# ==========================================================================
import mx.DateTime
import os, sys, time

import pywintypes
import win32serviceutil
import win32service
import win32event
from win32process import beginthreadex
from win32api import GetCurrentThreadId


try:
    p = os.path
    LOGFILE = p.join(p.dirname(p.abspath(__file__)), "logfile.txt")
except NameError:
    LOGFILE = "logfile.txt"

run = {'main': 1}


def log_time():
    ''' return a string nox() date '''
    return(mx.DateTime.now().Format("%Y-%m-%d %H:%M:%S"))

class Logger:
        def __init__(self, filename=LOGFILE):
            self.logfile = open(filename, "w+t")
            sys.stderr = sys.stdout = self.logfile

        def write(self, *msg):
            buf = "%s" % (msg,)
            self.log(buf)

        def log(self, summary, error=None):
            time = log_time()
            self.logfile.write("%s: %s\n" % (time, summary))
            if error:
                try:
                    self.logfile.write(format_exception(
                        error[0], error[1], error[2],
                        trailer='\n', limit=100))
                except:
                    self.logfile.write("%s: %s\n" % error[:2])
            self.logfile.flush()

        def __call__(self, summary, error=None):
            self.log(summary, error)

class MyService(win32serviceutil.ServiceFramework):
    _svc_name_ = "sample_serviceTest"
    _svc_display_name_ = "A sample serviceTest"
    def __init__(self, *args):
        try:
            self.log = Logger()
            self.log("Service starting, args=%s" % args)
            win32serviceutil.ServiceFramework.__init__(self, *args)
            self.ReportServiceStatus(win32service.SERVICE_START_PENDING)

            # Create an event which we will use to wait on.
            # The service stop request will set this event.
            self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

        except:
            self.log("Exception during service start", error=sys.exc_info())


    def SvcStop(self):
        try:
            # Tell the SCM we are starting the stop process.
            self.log("SvcStop: service must stop now!")
            global run
            run['main']=0

            hnd = []
            hnd.append(self.HndManagerOne)
            hnd.append(self.HndManagerTwo)

            # here start the error pywintypes.api_error in the event log on
Win2000 server
            while hnd:
                res = win32event.WaitForMultipleObjects( hnd, 0, 2000)
                if res >= win32event.WAIT_OBJECT_0 and res <
win32event.WAIT_OBJECT_0 + len(hnd):
                   del hnd[res - win32event.WAIT_OBJECT_0]


            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            win32event.SetEvent(self.hWaitStop)
        except:
            self.log("SvcStop()", sys.exc_info())

    def SvcDoRun(self):
        self.log("Service Running")
        try:
            self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
            self.log.write("MAIN Thread STARTED as:",
tCurrentThreadId()   )

            args = ()
            handle, id = beginthreadex(None, 0, self.ManagerOneThread, args,
0)
            self.HndManagerOne = handle
            handle, id = beginthreadex(None, 0, self.ManagerTwoThread, args,
0)
            self.HndManagerTwo = handle

            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            rc = win32event.WaitForSingleObject( self.hWaitStop,
win32event.INFINITE)

        except:
            self.log("SvcDoRun()", sys.exc_info())

        self.log("Service stopped")
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def ManagerTwoThread( self ):
        global run
        self.log.write("ManagerTwo: Thread STARTED with id :",
GetCurrentThreadId()  )
        time_sleep = 7
        while run['main']:
            time.sleep(time_sleep)

        self.log.write("ManagerTwo  thread Stopped")

    def ManagerOneThread( self ):
        global run
        self.log.write("ManagerOne: Thread STARTED with id :",
GetCurrentThreadId()  )
        time_sleep = 7
        while run['main']:
            time.sleep(time_sleep)

        self.log.write("ManagerOne thread STOPPED")

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(MyService)





More information about the Python-list mailing list