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

相关文章

Python实现变声器功能(萝莉音御姐音)

Python实现变声器功能(萝莉音御姐音)

登录百度AL开发平台 在控制台选择语音合成 创建应用 填写应用信息 在应用列表获取(Appid、API Key、Secret Key) 6. 安装pythonsdk 安装使用P...

python实现从wind导入数据

从wind导入到的数据的格式是instance。 如下载一系列资产在某一段时间的收盘价格。 一系列资产保存在list里面,一并下载。 日期格式为“2018-02-28”。 一个数字串儿表...

Python中使用bidict模块双向字典结构的奇技淫巧

快速入门 模块提供三个类来处理一对一映射类型的一些操作 'bidict', 'inverted', 'namedbidict' >>> import bidict...

python实现微信自动回复及批量添加好友功能

先给大家介绍下python微信自动回复功能 1.当收到好友消息时,自动回复 import random import itchat import requests import ti...

JupyterNotebook设置Python环境的方法步骤

JupyterNotebook设置Python环境的方法步骤

使用Python时,常遇到的一个问题就是Python和库的版本不同。Anaconda的env算是解决这个问题的一个好用的方法。但是,在使用Jupyter Notebook的时候,我却发现...