Laszlo Nagy wrote: > > Hello, > > I wrote a small program that uses xmlrpc over https. It should work as a > win32 application and as a win32 service too. There is a file called > Processor.py that contains the main thread of the program. It is called > from two files: win32_Application.py and win32_Service.py. The > application works perfectly. The service does not. I can start it from > the services mmc console, but it does not created logfiles, and does > nothing. It cannot be stopped and I have to kill pythonservice.exe. The > event log shows this: > > > Information: The AmazonOfferDownloaderService service has started. > Application error: Faulty application: python.exe, version: 0.0.0.0, > faulty modul: _ssl.pyd, version: 0.0.0.0, memory address: 0x00019b87. > > Is it possible that my _ssl.pyd is faulty? Please help me. > > Python version: 2.4.3 (#69, Mar 29 2006) > Windows version: Windows XP Professional, service pack 2, Hungarian (I > translated the above messages from the event log....) > > Thanks, > > Laszlo > > >>>> Here is the code for the application: > > import thread,threading,time > > from Processor import * > from servicelog import * > from win32_Config import * > > stopped = threading.Event() > stopped.clear() > processor = Processor(stopped) > thread.start_new_thread(processor.Process,()) > logger = getLogger('win32_Application') > logger.info("Staring as application. Please press CTRL+C to stop service.") > while 1: > try: > time.sleep(1) > except: > break > logger.info("Stopping application " + SERVICE_NAME) > stopped.set() > while not processor.stopped.isSet(): > logger.debug("Waiting for the processor to finish...") > time.sleep(1) > > logger.info("Application stopped.") > >>>> Here is the code for the service: > > from win32_Config import * > from Processor import * > from servicelog import * > > import win32serviceutil, win32service > import pywintypes, win32con, winerror > from win32event import * > from win32file import * > from win32pipe import * > from win32api import * > from ntsecuritycon import * > > import traceback > import thread,threading > > class Service(win32serviceutil.ServiceFramework): > _svc_name_ = SERVICE_NAME > _svc_display_name_ = SERVICE_DISPLAY > def __init__(self, args): > win32serviceutil.ServiceFramework.__init__(self, args) > self.stopped = threading.Event() > self.stopped.clear() > self.logger = getLogger(SERVICE_NAME) > > def SvcStop(self): > self.logger.info("Got SvcStop, trying to stop service...") > self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) > self.stopped.set() > > def SvcDoRun(self): > """Write an event log record - in debug mode we will also see > this message printed.""" > try: > import servicemanager > servicemanager.LogMsg( > servicemanager.EVENTLOG_INFORMATION_TYPE, > servicemanager.PYS_SERVICE_STARTED, > (self._svc_name_, '') > ) > self.logger.info("Started.") > self.logger.debug("Creating processor instance") > processor = Processor(self.stopped) > self.logger.debug("Starting processor thread") > thread.start_new_thread(processor.Process,()) > self.logger.debug("Waiting for the processor thread to finish") > self.stopped.wait() > self.logger.debug("Stopping") > time.sleep(1) > while not processor.stopped.isSet(): > > self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000) > time.sleep(5) > servicemanager.LogMsg( > servicemanager.EVENTLOG_INFORMATION_TYPE, > servicemanager.PYS_SERVICE_STOPPED, > (self._svc_name_, "") > ) > self.logger.info("Stopped") > except: > self.logger.error('',exc_info = sys.exc_info()) > > > if __name__=='__main__': > win32serviceutil.HandleCommandLine(Service) > > I see some problems. You don't use time sleep in services. You wait for a stop signal and if you don't get it by the time you reach your timeout, you loop. I'm not sure what you are trying to accomplish with the threading module, but you should start with a simple service first. I don't think you will need the threading at all. Here is a skeleton that might help:
class Service(win32serviceutil.ServiceFramework): def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.timeout=5000 # 5000 milliseconds or 5 seconds #--------------------------------------------------------------------- # 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) return def SvcStop(self): #--------------------------------------------------------------------- # Before we do anything, tell SCM we are beginning the stop process. #--------------------------------------------------------------------- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #--------------------------------------------------------------------- # And set my event, note you don't stop here, you just set the flag # so that you will exit from your while loop in SvcDoRun #--------------------------------------------------------------------- win32event.SetEvent(self.hWaitStop) return def SvcDoRun(self): import servicemanager #--------------------------------------------------------------------- # Make entry in the event log that this service started #--------------------------------------------------------------------- servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) while 1: #------------------------------------------------------------------- # Wait for service stop signal, if I timeout, loop again #------------------------------------------------------------------- rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout) # # Check to see if self.hWaitStop happened # if rc == win32event.WAIT_OBJECT_0: # # Stop signal encountered # break else: # # Do your work here # pass #--End while-- #--------------------------------------------------------------------- # Log stopped message to EventLog #--------------------------------------------------------------------- servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, '')) return Note: I pieced this together from a working service, it has NOT been tested. It should be VERY close. If you don't have it already you might want to pick up a copy of Python Programming on Win32 by Mark Hammond and Andy Robinson. It helped me a LOT. -Larry -- http://mail.python.org/mailman/listinfo/python-list