如何使用cx_Freeze安装Python Windows服务?

时间:2015-07-15 20:29:18

标签: python windows service cx-freeze

我目前有一个Python文件,当使用python file_name.py运行时会安装一个Windows服务,该服务可以在应用程序日志下的事件查看器中查看,并可以使用sc stop service_name停止。但是,使用cx_Freeze转换为可执行文件时,可执行文件运行时没有错误,但服务不再安装。如果我自己运行可执行文件,如果我运行service_name.exe --install service_name,或者运行sc create service_name binPath = service_path

,则会发生这种情况。

我的setup.py文件类似于:

from cx_Freeze import setup, Executable

options = {
'build_exe': {
    'packages': ['packagename'],
    'includes': ['ServiceHandler', 'cx_Logging']}
}

setup(name='cx_FreezeSampleService',
  version='0.1',
  description='Sample cx_Freeze Windows serice',
  executables=Executable('Config.py', base='Win32Service',
           targetName='cx_FreezeSampleService.exe'),
  options=options
  )

我的Config.py看起来像:

NAME = 'cx_FreezeSampleService%s'
DISPLAY_NAME = 'cx_Freeze Sample Service - %s'
MODULE_NAME = 'ServiceHandler'
CLASS_NAME = 'Handler'
DESCRIPTION = 'Sample service description'
AUTO_START = True
SESSION_CHANGES = False

最后,我的ServiceHandler.py看起来像:

class Handler(object):
 def Initialize(self, Config):
    pass

 def Run(self):
    #code to run service

 def Stop(self):
    #code to stop service

此代码几乎完全遵循cx_Freeze源代码(https://bitbucket.org/anthony_tuininga/cx_freeze/src/1282b6b6ee637738210113dd88c3c198d475340f/cx_Freeze/samples/service/?at=default)中的示例,但这个和示例似乎都不能用于实际安装服务。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个老问题,但我在开发人员的帮助下设法让它作为一个简单的烧瓶应用程序的窗口服务工作。 [https://github.com/marcelotduarte/cx_Freeze/tree/master/cx_Freeze/samples/service]

您必须设置您想要性能的所有 Windows 服务操作。 这就是 ServiceHandler.py 作为模板的样子,您仍然需要调整以运行您的应用程序。

"""
Implements a simple service using cx_Freeze.
See below for more information on what methods must be implemented and how they
are called.
"""

import threading
import os
import sys
import cx_Logging




class Handler:

    # no parameters are permitted; all configuration should be placed in the
    # configuration file and handled in the Initialize() method
    def __init__(self):
        self.stopEvent = threading.Event()
        self.stopRequestedEvent = threading.Event()

    # called when the service is starting
    def initialize(self, configFileName):
        self.directory = os.path.dirname(sys.executable)
        cx_Logging.StartLogging(os.path.join(self.directory, "teste.log"), cx_Logging.DEBUG)
        #pass

    # called when the service is starting immediately after Initialize()
    # use this to perform the work of the service; don't forget to set or check
    # for the stop event or the service GUI will not respond to requests to
    # stop the service
    def run(self):
        cx_Logging.Debug("stdout=%r", sys.stdout)
        sys.stdout = open(os.path.join(self.directory, "stdout.log"), "a")
        sys.stderr = open(os.path.join(self.directory, "stderr.log"), "a")
        self.stopRequestedEvent.wait()
        self.stopEvent.set()

    # called when the service is being stopped by the service manager GUI
    def stop(self):
        self.stopRequestedEvent.set()
        self.stopEvent.wait()