Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用cx\U Freeze安装Python Windows服务?_Python_Windows_Service_Cx Freeze - Fatal编程技术网

如何使用cx\U Freeze安装Python Windows服务?

如何使用cx\U Freeze安装Python Windows服务?,python,windows,service,cx-freeze,Python,Windows,Service,Cx Freeze,我目前有一个Python文件,当使用Python文件_name.py运行时,它会安装一个Windows服务,该服务可以在应用程序日志下的事件查看器中查看,并且可以使用sc stop service_name停止。但是,当使用cx_Freeze转换为可执行文件时,可执行文件运行时不会出现错误,但服务不再安装。如果我只运行可执行文件本身,如果我运行service_name.exe--install service_name,或者如果我运行sc create service_name binPath=

我目前有一个Python文件,当使用Python文件_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
  )
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
class Handler(object):
 def Initialize(self, Config):
    pass

 def Run(self):
    #code to run service

 def Stop(self):
    #code to stop service
我的Config.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
  )
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
class Handler(object):
 def Initialize(self, Config):
    pass

 def Run(self):
    #code to run service

 def Stop(self):
    #code to stop service
最后,我的ServiceHandler.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
  )
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
class Handler(object):
 def Initialize(self, Config):
    pass

 def Run(self):
    #code to run service

 def Stop(self):
    #code to stop service
这段代码几乎完全遵循cx_Freeze source code here()中的示例,但这段代码和示例在实际安装服务时似乎都不起作用


提前谢谢你

这是一个老问题,但在开发人员的帮助下,我设法让它作为一个窗口服务用于一个简单的flask应用程序。 [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()

我发现cx_冻结示例中存在错误。函数:
初始化、运行、停止
必须用小写字母定义。例如,服务本身已注册到
cx\u FreezeSampleService.exe--install test