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

yipeiwu_com6年前Python基础

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

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

相关文章

简单介绍利用TK在Python下进行GUI编程的教程

简单介绍利用TK在Python下进行GUI编程的教程

我想要向您介绍能想像到的开始 GUI 编程的最简单方法,就是使用 Scriptics 的 TK 和 Tkinter 封装器。我们将与 developerWorks 中的 “Python...

在Python中使用异步Socket编程性能测试

OK,首先写一个python socket的server段,对开放三个端口:10000,10001,10002.krondo的例子中是每个server绑定一个端口,测试的时候需要分别开3...

巧用Python装饰器 免去调用父类构造函数的麻烦

先看一段代码: 复制代码 代码如下: class T1(threading.Thread): def __init__(self, a, b, c): super(T1, self)._...

python 分离文件名和路径以及分离文件名和后缀的方法

分离路径和文件名: os.path.split() 区分文件的名字和后缀: os.path.splitext() import os file_path = "D:/test/t...

Python学习笔记基本数据结构之序列类型list tuple range用法分析

本文实例讲述了Python学习笔记基本数据结构之序列类型list tuple range用法。分享给大家供大家参考,具体如下: list 和 tuple list:列表,由 []...