我已阅读How to create a service in Python in Windows和opening another program through windows service using python。
现在我有以下代码:
# 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
和其他代码:
# 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 ''
现在,我的问题output_directory = config.get_prop('dump.path')
SvcDoRun
未运行,为什么?如果我在其他pyfile中使用它,它可以运行。
答案 0 :(得分:0)
现在我已经解决了这个问题,原因是os.getcwd()
在函数C:\Python27\Lib\site-packages\win32
中返回SvcDoRun
。