温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python脚本作为Windows服务启动代码详解

发布时间:2020-09-17 08:40:23 来源:脚本之家 阅读:151 作者:laozhang 栏目:开发技术

我们首先来看下全部代码:

# -*- coding: cp936 -*- 
import win32serviceutil 
import win32service 
import win32event 
class test1(win32serviceutil.ServiceFramework): 
  _svc_name_ = "test_python" 
  _svc_display_name_ = "test_python" 
  def __init__(self, args): 
    win32serviceutil.ServiceFramework.__init__(self, args) 
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 
  def SvcStop(self): 
    # 先告诉SCM停止这个过程 
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
    # 设置事件 
    win32event.SetEvent(self.hWaitStop) 
  def SvcDoRun(self): 
    # 等待服务被停止 
    win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) 
if __name__=='__main__': 
  win32serviceutil.HandleCommandLine(test1)

这里注意,如果你需要更改文件名,比如将win32serviceutil.HandleCommandLine(test1)中的test1更改为你的文件名,同时class也需要和你的文件名一致,否则会出现服务不能启动的问题。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI