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

作者:laozhang 时间:2021-09-11 00:09:26 

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


# -*- 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也需要和你的文件名一致,否则会出现服务不能启动的问题。

标签:python,Windows,服务启动
0
投稿

猜你喜欢

  • Python 十大经典排序算法实现详解

    2021-10-01 02:55:24
  • Pandas的read_csv函数参数分析详解

    2021-06-02 13:40:15
  • python 爬取壁纸网站的示例

    2022-07-10 13:29:54
  • python得到windows自启动列表的方法

    2023-12-18 14:49:13
  • python中自带的三个装饰器的实现

    2021-10-28 02:31:52
  • Python深度学习实战PyQt5布局管理项目示例详解

    2023-03-11 08:47:37
  • Python提高运行速度工具之Pandarallel的使用教程

    2021-07-16 20:14:09
  • python语言中pandas字符串分割str.split()函数

    2022-01-30 16:55:56
  • Python模块学习 datetime介绍

    2023-08-15 16:01:18
  • python连接远程ftp服务器并列出目录下文件的方法

    2023-10-20 10:35:04
  • python实现获取Ip归属地等信息

    2022-10-24 00:13:31
  • Python实现的各种常见分布算法示例

    2021-12-06 18:26:15
  • 带中英文翻译功能的收藏夹

    2008-07-31 11:33:00
  • python使用cookielib库示例分享

    2022-09-22 13:53:37
  • python中使用enumerate函数遍历元素实例

    2021-05-08 04:56:41
  • HTML标签tbody的用法

    2009-11-02 10:11:00
  • python中的编码和解码及\\x和\\u问题

    2023-11-02 14:46:31
  • Python基于Socket实现简单聊天室

    2022-06-13 11:56:51
  • 用于ETL的Python数据转换工具详解

    2022-11-09 18:29:05
  • JavaScript中Infinity(无穷数)的使用和注意事项

    2023-08-21 00:59:13
  • asp之家 网络编程 m.aspxhome.com