簡體   English   中英

如何使用 cx_Freeze 安裝 Python Windows 服務?

[英]How to install a Python Windows service using 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幾乎既不是示例,也不是默認示例)實際安裝服務。

先感謝您!

這是一個老問題,但我設法在開發人員的幫助下將它作為一個簡單的燒瓶應用程序的窗口服務工作。 [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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM