2

I am trying to make python script run as a windows service.

Just to have a little understanding, I have taken this code from here.

I have installed the following things:

  1. python2.7 (64 bit)
  2. pywin32.exe. (64 bit)

However, the script is not able to find the following things:

  1. win32event.CreateEvent
  2. win32event.WAIT_OBJECT_0
  3. win32event.WaitForSingleObject(self.hWaitStop, 5000)
  4. win32service.SERVICE_STOP_PENDING)
  5. win32event.SetEvent

Though I am not getting any error messages for import but I am getting error messages for the aforementioned functions.

import win32service  
import win32serviceutil  
import win32event  

class PySvc(win32serviceutil.ServiceFramework):  
# you can NET START/STOP the service by the following name  
_svc_name_ = "PySvc"  
# this text shows up as the service name in the Service  
# Control Manager (SCM)  
_svc_display_name_ = "Python Test Service"  
# this text shows up as the description in the SCM  
_svc_description_ = "This service writes stuff to a file"  

def __init__(self, args):  
    win32serviceutil.ServiceFramework.__init__(self,args)  
    # create an event to listen for stop requests on  
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

# core logic of the service     
def SvcDoRun(self):  
    import servicemanager  

    f = open('test.dat', 'w+')  
    rc = None  

    # if the stop event hasn't been fired keep looping  
    while rc != win32event.WAIT_OBJECT_0:  
        f.write('TEST DATA\n')  
        f.flush()  
        # block for 5 seconds and listen for a stop event  
        rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)  

    f.write('SHUTTING DOWN\n')  
    f.close()  

# called when we're being shut down      
def SvcStop(self):  
    # tell the SCM we're shutting down  
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
    # fire the stop event  
    win32event.SetEvent(self.hWaitStop)  

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

1 Answer 1

3

I tried to reinstall win32 from sourceforge.net site. But it didn't work out.

Finally I tried the pip command, and it worked like charm.

pip install pypiwin32
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.