Windows 服务编译与 pyinstaller 问题

Windows service compile with pyinstaller issue

我已经实现了一个windows服务,这个服务在用pyinstaller编译之前没有问题,但是在服务启动命令之后它给出了1053错误。

Windows服务代码:

import sys
import win32service
import win32event
import socket
import win32api
import win32serviceutil


class AppServerSvc(win32serviceutil.ServiceFramework):
    _svc_name_ = "test"
    _svc_display_name_ = "test"
    _stoped = False

    def __init__(self, *args):
        win32serviceutil.ServiceFramework.__init__(self, *args)
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self._stoped = True

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        self.main()

    def main(self):
        while True:
            if self._stoped:
                break
            pass

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

我终于解决了这个问题,link 到 windows 服务中的一个引用有问题。这是 py2exe 的正确配置,它解决了我的问题:

opts = {'py2exe': {
'dll_excludes': ['libzmq.pyd', 'OLEAUT32.dll', 'USER32.dll', 'SHELL32.dll', 'ole32.dll',
                 'MSVCP90.dll', 'ADVAPI32.dll', 'NETAPI32.dll', 'WS2_32.dll', 'GDI32.dll',
                 'VERSION.dll', 'KERNEL32.dll', 'WINSPOOL.DRV', 'mfc90.dll', 'ntdll.dll'],
'includes': ['UserList', 'UserString', 'commands', 'zmq.backend.cython'],
'dist_dir': "dist"
}}

setup(service=[service], options=opts, zipfile=None,data_files=[(os.path.join(os.getcwd(), 'dist'), (zmq.libzmq.__file__,))])

要在 pyInstaller 生成的 exe 中使用我的 win32serviceutil.ServiceFramework class,我需要将一些 .pyd 文件添加到包含 exe 的目录中,例如win32service.pyd 和 win32event.pyd。假设您已经为您的库使用了标准安装路径,可以在此处找到这些文件:

C:\Python27\Lib\site-packages\win32