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

相关文章

Pytorch的mean和std调查实例

如下所示: # coding: utf-8 from __future__ import print_function import copy import click impor...

Django模板语言 Tags使用详解

Tags # 普通for循环 <ul> {% for user in user_list %} <li>{{ user.name }}</li&...

Python实现定时任务

Python下实现定时任务的方式有很多种方式。下面介绍几种 循环sleep: 这是一种最简单的方式,在循环里放入要执行的任务,然后sleep一段时间再执行。缺点是,不容易控制,而且sl...

Python利用matplotlib做图中图及次坐标轴的实例

Python利用matplotlib做图中图及次坐标轴的实例

图中图 准备数据 import matplotlib.pyplot as plt fig = plt.figure() x = [1, 2, 3, 4, 5, 6, 7] y =...

完美解决python3.7 pip升级 拒绝访问问题

完美解决python3.7 pip升级 拒绝访问问题

python3.7 pip升级 拒绝访问 解决方案 pip install --upgrade pip --user ps:下面看下python中的for循环加强 #先执行外...