无法 运行 Python 中最小的 windows 服务

Unable to run smallest windows service in Python

根据 的回答,我在 Python 中创建了一个最小的 Windows 服务。

然后我运行成功了:

python.exe smallest_service.py install

python.exe smallest_service.py start

有错误信息

Starting service SmallestPythonService

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

我的Python版本是Python 3.9.6

pip freeze
pypiwin32==223
pywin32 @ file:///C:/Users/me/Downloads/pywin32-301-cp39-cp39-win_amd64.whl

我该如何解决?

如果这对您不起作用。使用 NSSM 可能会很有趣。这是一段简单的代码,可以将任何 py 文件变成 windows 服务。

我还发现大多数服务创建者只使用 32 位Python,所以这可能也很有趣。

以下是您可能会看到的场景列表 -> 启动服务时出错:服务未及时响应启动或控制请求。

缺少 DLL 文件: 当您的计算机上缺少 DLL 文件时,会发生另一个错误实例,该文件也被许多其他应用程序使用。如果此 DLL 文件存在冲突或根本不存在,您将遇到错误消息。

Corrupt/missing 系统文件: 出现此问题的另一个实例是因为您的计算机上存在损坏或丢失的系统文件。如果 Windows 的安装不正确并且有问题,您将遇到许多问题,包括正在讨论的错误消息。

已过时 Windows: 微软官方在他们的官方网站上承认了这个错误信息,甚至发布了一个临时修补程序来解决这个问题。但是,最近他们删除了修补程序并指示用户升级到 Windows 的最新版本。

解决方案

我们应该尝试的第一件事是通过注册表编辑器更改服务的超时设置。每当请求启动服务时,都会使用预定义的值启动计时器。如果服务没有在此时间范围内启动,则会出现错误消息。在此解决方案中,我们将导航到您计算机的注册表并更改值。如果它不存在,我们将为它创建一个新密钥。

-> 按Windows + R,在对话框中输入“regedit”并回车。

  • Once in the registry editor, navigate to the following file path: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
    -> search for the key of ‘ServicesPipeTimeout’. If you find it already there, you can move to directly edit. However, if you don’t find the entry, select Control, right-click on any space present at the right side of the screen and select New > DWORD -> Name the key as ‘ServicesPipeTimeout’ and set the value as 180000 (You can also right-click the value and click Modify if the option to set the value didn’t come in your case. -> Save changes and exit. Restart your computer completely and then try launching the service. Check if the issue is resolved.

解决方案 2:获取应用程序的所有权

我们遇到的另一种罕见情况是没有应用程序的所有权导致应用程序无法正确执行服务。这是有道理的,就好像应用程序没有足够的提升访问权限,它将无法 send/read 响应 to/from 服务(特别是如果它是系统服务)。在本文中,我们将导航到应用程序的可执行文件,然后将所有权更改为我们的用户名。如果成功,这将解决出现错误 1053 的问题。

奖金提示

确保 .NET Frameworks 同步:如果您尝试启动的 application/service 是在另一个 Framework 而不是托管机器上,您将遇到问题。确保框架同步。