栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Windows中将Python脚本作为服务运行?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Windows中将Python脚本作为服务运行?

可以。我使用ActivePython随附的pythoncom库或可以与pywin32(适用于Windows的Python扩展)一起安装的方式来执行此操作。

这是简单服务的基本框架:

import win32serviceutilimport win32serviceimport win32eventimport servicemanagerimport socketclass AppServerSvc (win32serviceutil.Serviceframework):    _svc_name_ = "TestService"    _svc_display_name_ = "Test Service"    def __init__(self,args):        win32serviceutil.Serviceframework.__init__(self,args)        self.hWaitStop = win32event.CreateEvent(None,0,0,None)        socket.setdefaulttimeout(60)    def SvcStop(self):        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)        win32event.SetEvent(self.hWaitStop)    def SvcDoRun(self):        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,        servicemanager.PYS_SERVICE_STARTED,        (self._svc_name_,''))        self.main()    def main(self):      pass         if __name__ == '__main__':  win32serviceutil.HandleCommandLine(AppServerSvc)

你的代码将进入

main()
方法中-通常带有某种无限循环,可能会通过检查在
SvcStop
方法中设置的标志而中断



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号