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

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

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

所以我用 Python 3.7 构建了下面显示的代码。 完整的代码可以在 Github.

上找到

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

这使用命令:

安装服务:

运行 服务:

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

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

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

我在 Whosebug 和 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)

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

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