无法启动用Python编写的Windows服务(win32serviceutil)

tas*_*tas 17 python windows windows-services pywin32

我正在尝试启动一个简单的服务示例:

someservice.py:

import win32serviceutil 
import win32service 
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "display service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
Run Code Online (Sandbox Code Playgroud)

我跑的时候

python someservice.py install
Run Code Online (Sandbox Code Playgroud)

一切都很好,服务出现在Windows服务列表中,但是

python someservice.py start
Run Code Online (Sandbox Code Playgroud)

失败,出现"错误1053:服务未及时响应启动或控制请求",但没有任何延迟.

我用谷歌搜索了一个解决方案,它表示pythonservice.exe无法找到时会发生这种情况python27.dll.它实际上不可能,所以我说C:\Python27PATH.现在pythonservice.exe运行正常,但错误1053仍然存在.

我在Windows 7旗舰版上使用pywin32 216运行Python 2.7.2并具有管理员权限.

mpa*_*paf 12

另外,感谢指出它可能是一个DLL问题,这使我找到了正确的解决方案.

您需要做的是将Python27添加到SYSTEM PATH,而不是添加到USER PATH,因为默认情况下python服务将作为"LocalSystem"安装,因此当它尝试启动它时使用SYSTEM PATH变量 - 这是为什么你可以从命令提示符运行它,你的用户路径是正确的.

希望能帮助到你!

  • 在这种情况下,将这些目录添加到系统路径可能会很有用:`C:\ Python27\Lib\site-packages\win32`和`C:\ Python27\Lib\site-packages\pywin32_system32`.这将让您更轻松地使用pythonservice. (6认同)

Ale*_*lex 6

我也遇到了这个问题,并且能够通过将以下内容添加到我的"__main__"执行块来解决它:

if len(sys.argv) == 1:
    servicemanager.Initialize()
    servicemanager.PrepareToHostSingle(RouterService)
    servicemanager.StartServiceCtrlDispatcher()
else:
    win32serviceutil.HandleCommandLine(RouterService)
Run Code Online (Sandbox Code Playgroud)

(不要忘记在文件顶部导入 servicemanager)。

我认为问题在于 Windows 服务管理器运行不带参数的可执行文件(默认情况下),在这种情况下,需要正确告知应用程序启动服务,但SvcDoRun似乎不会自动调用。

正如其他人提到的,如果从命令行运行它,则确实需要路径映射。在我的应用程序中,我冻结了服务并cx_freeze使用可执行文件来安装服务,以便包含所有依赖项。


Dom*_*ugh 4

我相信通过更改SvcDoRun方法可以解决您的问题

   def  SvcDoRun(self):
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
Run Code Online (Sandbox Code Playgroud)

   def  SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
Run Code Online (Sandbox Code Playgroud)