【问题标题】:pyinstaller Error starting service: The service did not respond to the start or control request in a timely fashionpyinstaller错误启动服务:服务没有及时响应启动或控制请求
【发布时间】:2020-09-24 10:51:23
【问题描述】:

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

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

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

这个使用命令:

安装服务:

  • python jis53_backup.py 安装

运行服务:

  • python jis53_backup.py 启动

当我现在使用带有命令的 pyinstaller 编译这段代码时:

  • pyinstaller -F --hidden-import=win32timezone jis53_backup.py

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

错误启动服务:服务没有响应启动或 及时控制请求

我在 Stackoverflow 和 Google 上浏览了多篇与此错误相关的帖子,但均未成功。我没有选择在需要运行此服务的 PC 上安装 python 3.7 程序。这就是我们尝试构建 .exe 的原因。

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

路径定义图片:

我还复制了 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)

【问题讨论】:

  • 这个运气好吗?
  • @njho,到目前为止还没有运气。我可以通过 python 运行服务,但无法通过 exe 运行。
  • 嘿@peter 感谢您的回复。我还在玩我的,但我已经让它工作了一半。检查 Python 环境变量是否也在您的 SYSTEM 路径中。这就是为我解决的问题。让我知道它是否有效
  • @njho,我的系统路径中有 python,但它仍然不适合我。
  • 这个运气好吗?我有同样的问题。想知道它是否是 python3.7 的东西......因为有很多使用 3.6 的例子

标签: python python-3.x pyinstaller


【解决方案1】:

在使用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 编译后,现在可以正常工作,没有任何错误。看起来当我们做动态路径时,它没有得到文件的实际路径,因此它给出了错误。

希望这也能解决您的问题。谢谢

【讨论】:

  • 你是我的英雄。你是怎么想出来的..我还没有找到关于失败的任何细节?谢谢,这东西让我生气了。 :)
  • How did you figure it out 我将我的代码分成小部分,然后使用 pyinstaller 并意识到动态路径是唯一造成问题的点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 2014-08-05
  • 2011-01-05
  • 1970-01-01
  • 2014-04-08
  • 2011-12-29
相关资源
最近更新 更多