简体   繁体   中英

Very Weird windows service behavior when using py2exe

I coded out a windows service in python to write some text to a file continuously and installed it and ran it and it works fine. Now if I try to convert my python windows service script into an executable (.exe) using py2exe. The .exe installs fine as a service but when I try to start it I get the error "The server did not respond to the start ......in timely fashion". Is this got something to do with py2exe destroying information in my python script. How do I go around this? (I am trying to convert it to a .exe because I want to distribute it).

My python script is as follows:

import win32service
import win32serviceutil
import win32event



class clear_queue(win32serviceutil.ServiceFramework):

_svc_name_ = "avant"

_svc_display_name_ = "avant"

_svc_description_ = "Elegant file writer"

def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self,args)

    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)


def SvcDoRun(self):

    import servicemanager;
    fil = open("C:/Users/u/Desktop/c99/user.txt",'r+');

    rc = win32event.WaitForSingleObject(self.hWaitStop, 64)
    while rc != win32event.WAIT_OBJECT_0:

        fil.write("george\n");
        rc = win32event.WaitForSingleObject(self.hWaitStop, 64)


def SvcStop(self):

    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

    win32event.SetEvent(self.hWaitStop)

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

看一下http://tools.cherrypy.org/wiki/WindowsService上的示例,似乎您需要添加self.ReportServiceStatus(win32service.SERVICE_STOPPED)作为SvcStop方法的最后一行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM