我的目标是创建一个可用于安装Windows系统服务的单一(或多个).exe。目前这只是一个概念证明,所以我所有的服务都是写入一个文件来确认它的存在。涉及py2exe&python服务的问题
当我从.py文件调用服务时,它会安装并正常运行。当我使用py2exe制作一个.exe文件时,它运行良好,但是我在Windows事件日志(应用程序)中收到了一些消息,指出找不到某些库。有时候Windows会在启动后停止服务,有时Windows会忽略“找不到模块”的错误。所有这些都在XP SP3机器上进行。
当我将相同的.exe编译为w/py2exe到Win 7 SP1机器时,win7会告诉我,没有python27.dll就无法运行.exe。所以我在.exe的cwd中移动python27.dll,然后Win 7告诉我它没有加载.dll文件。当我尝试在XP SP2机器上运行.exe时,XP告诉我该文件无法加载。
这里是对应的代码:
PySvc.py(这是实际的服务,PySvc.py安装用于从提示安装它的字符串:
import win32service
import win32serviceutil
import win32event
import servicemanager
class PySvc(win32serviceutil.ServiceFramework):
_svc_name_ = "PySvc"
_svc_display_name_ = "Python Test"
_svc_description_ = "Service Test"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# Meat and potatos. This is the actual code that's run. Currently testing
# to see behavior inside and outside of loop.
def SvcDoRun(self):
f = open('test.dat', 'w+')
rc = None
f.write('OUTSIDE L00P\n')
# continue iteration if stop event not recieved
while rc != win32event.WAIT_OBJECT_0:
f.write('BEAUTIFUL TEST DATA NEW INSIDE L00P\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 on shut down
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PySvc)
以下是对于“setup.py”的代码。setup.py py2exe是用于创建.exe文件的字符串。
from distutils.core import setup
import py2exe
setup(
name = 'PySvc',
description = 'Service Test',
version = '1.00.00',
service = ['PySvc'],
console = ['PySvc.py'],
zipfile=None,
options = {
"py2exe":{
"includes":"win32service,win32serviceutil,win32event,servicemanager",
},
},
)
由于本地机器,但失败的.exe文件的明显的成功在其他几家机器(没有安装python),我目前倾向于认为这是导入问题或进口编译方式。我会永远感激,如果任何人都可以提供一些见解,为什么这个.exe不想要像它应该的行为。感谢您的时间。