0

I have read How to create a service in Python in Windows and opening another program through windows service using python.

Now I have the below code:

# server.py
class ServerManager(win32serviceutil.ServiceFramework):
    _svc_name_ = 'test'
    _svc_display_name_ = 'test_display'

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self._poll_intvl = 20

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        # self.timeout = 100
        while 1:
            rc = win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
            if rc == win32event.WAIT_OBJECT_0:
                break
            else:
                import config
                output_directory = config.get_prop('dump.path')

                with open(r'e:\time.txt','a') as f:
                    f.write('%s %s'%(str(output_directory), ''))
                time.sleep(self._poll_intvl)
        return


    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        return

and other code:

# config.py
import ConfigParser
import os

def _read_config(configPath):
    cd = {}
    cf = ConfigParser.ConfigParser()
    cf.read(configPath)
    sessions = cf.sections()
    for session in sessions:
        options = cf.options(session)
        for option in options:
            key = session + "." + option
            value = cf.get(session, option)
            cd[key.lower()] = value
    return cd

g_propDict = {}
def get_prop(key):
    global g_propDict
    if key not in g_propDict:
        propFilePath = os.path.join(os.getcwd(), 'config.ini')
        g_propDict = _read_config(propFilePath)

    if key in g_propDict:
        return g_propDict[key]
    return ''

Now, my problem is output_directory = config.get_prop('dump.path') in SvcDoRun is not run, why? If I use it in other pyfile, it can run.

Community
  • 1
  • 1
thinkerou
  • 1,781
  • 5
  • 17
  • 28

1 Answers1

0

Now I have solved this question, the reason is os.getcwd() returns C:\Python27\Lib\site-packages\win32 in the function SvcDoRun.

thinkerou
  • 1,781
  • 5
  • 17
  • 28