python - pyinstaller 启动服务时出错 : The service did not respond to the start or control request in a timely fashion

标签 python python-3.x pyinstaller

几天以来,我一直在寻找解决方案,但没有成功。
我们有一个 Windows 服务构建来将一些文件从一个位置复制到另一个位置。

所以我用 Python 3.7 构建了如下所示的代码。
完整的编码可以在 Github 上找到。 .

当我使用 python 运行服务时一切正常,我可以安装服务并启动服务。

这使用命令:

安装服务:

  • python jis53_backup.py 安装

  • 运行服务:
  • python jis53_backup.py 开始

  • 当我现在使用 pyinstaller 和命令编译此代码时:
  • pyinstaller -F --hidden-import=win32timezone jis53_backup.py

  • 创建 exe 后,我可以安装该服务,但在尝试启动该服务时出现错误:

    Error starting service: The service did not respond to the start or control request in a timely fashion



    我已经在 Stackoverflow 和 Google 上浏览了多篇与此错误相关的帖子,但都没有成功。我没有选择在需要运行此服务的 PC 上安装 python 3.7 程序。这就是我们试图获得 .exe 版本的原因。

    我已确保根据我在不同问题中找到的信息更新路径。

    路径定义的图像:

    enter image description here

    我还复制了 pywintypes37.dll 文件。

    从 -> Python37\Lib\site-packages\pywin32_system32

    到 -> Python37\Lib\site-packages\win32

    有没有人对如何让这个工作有任何其他建议?
    '''
        Windows service to copy a file from one location to another
        at a certain interval.
    '''
    import sys
    import time
    from distutils.dir_util import copy_tree
    
    import servicemanager
    import win32serviceutil
    
    import win32service
    from HelperModules.CheckFileExistance import check_folder_exists, create_folder
    from HelperModules.ReadConfig import (check_config_file_exists,
                                          create_config_file, read_config_file)
    from ServiceBaseClass.SMWinService import SMWinservice
    
    sys.path += ['filecopy_service/ServiceBaseClass',
                 'filecopy_service/HelperModules']
    
    
    class Jis53Backup(SMWinservice):
        _svc_name_ = "Jis53Backup"
        _svc_display_name_ = "JIS53 backup copy"
        _svc_description_ = "Service to copy files from server to local drive"
    
        def start(self):
            self.conf = read_config_file()
            if not check_folder_exists(self.conf['dest']):
                create_folder(self.conf['dest'])
    
            self.isrunning = True
    
        def stop(self):
            self.isrunning = False
    
        def main(self):
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            while self.isrunning:
                # Copy the files from the server to a local folder
                # TODO: build function to trigger only when a file is changed.
                copy_tree(self.conf['origin'], self.conf['dest'], update=1)
                time.sleep(30)
    
    
    if __name__ == '__main__':
        if sys.argv[1] == 'install':
            if not check_config_file_exists():
                create_config_file()
    
        if len(sys.argv) == 1:
            servicemanager.Initialize()
            servicemanager.PrepareToHostSingle(Jis53Backup)
            servicemanager.StartServiceCtrlDispatcher()
        else:
            win32serviceutil.HandleCommandLine(Jis53Backup)
    

    最佳答案

    使用 pyinstaller 编译后我也遇到了这个问题.对我来说,问题是我以动态方式使用配置和日志文件的路径,例如:

    curr_path = os.path.dirname(os.path.abspath(__file__)) 
    configs_path = os.path.join(curr_path, 'configs', 'app_config.json')
    opc_configs_path = os.path.join(curr_path, 'configs', 'opc.json')
    log_file_path = os.path.join(curr_path, 'logs', 'application.log')
    
    当我使用 python service.py install/start 启动服务时,这工作正常.但是在使用 pyinstaller 编译之后,它总是给我不及时启动的错误。
    为了解决这个问题,我将所有动态路径都设为静态,例如:
    configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\app_config.json'
    opc_configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\opc.json'
    debug_file = 'C:\\Program Files (x86)\\ScantechOPC\\logs\\application.log'
    
    通过 pyinstaller 编译后,它现在工作正常,没有任何错误。看起来当我们做动态路径时,它没有得到文件的实际路径,因此会出错。
    希望这也能解决您的问题。谢谢

    关于python - pyinstaller 启动服务时出错 : The service did not respond to the start or control request in a timely fashion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53962603/

    相关文章:

    Python:从标准输入读取gzip

    python - 调用使用 PyInstaller(包括 PyQt4)制作的 .exe 时出现运行时错误

    python - 如何修复错误 :'range' object is not callable in python3.6

    python - tkinter 中的 SQL 查询结果

    python - 如何将终端输出显示到 PyQt 中的文本框

    python-2.7 - PyInstaller: "No module named Tkinter"

    audio - 创建可执行文件pygame时遇到问题-pyinstaller

    python - python google app engine中的断开链接图像

    python - SQLAlchemy 级联删除自动映射的通用数据模型架构

    python - 如何立即重新引发任何工作线程中抛出的异常?