以windows service方式运行Python程序的方法

yipeiwu_com5年前Python基础

本文实例讲述了以windows service方式运行Python程序的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
# coding: utf-8 
# SmallestService.py 
# 
# A sample demonstrating the smallest possible service written in Python.
import win32serviceutil 
import win32service 
import win32event 
import time 
class SmallestPythonService(win32serviceutil.ServiceFramework): 
  _svc_name_ = "SmallestPythonService" 
  _svc_display_name_ = "The smallest possible Python Service" 
  def __init__(self, args): 
    win32serviceutil.ServiceFramework.__init__(self, args) 
    # Create an event which we will use to wait on. 
    # The "service stop" request will set this event. 
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 
  def SvcStop(self): 
    # Before we do anything, tell the SCM we are starting the stop process. 
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
    # And set my event. 
    win32event.SetEvent(self.hWaitStop) 
  def SvcDoRun(self): 
    #把你的程序代码放到这里就OK了 
    f=open('d:\\log.txt','w',0) 
    f.write(time.ctime(time.time())) 
    f.close() 
    win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) 
if __name__=='__main__': 
  win32serviceutil.HandleCommandLine(SmallestPythonService)  
  # 括号里的名字可以改成其他的,必须与class名字一致;

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python通过robert、sobel、Laplace算子实现图像边缘提取详解

python通过robert、sobel、Laplace算子实现图像边缘提取详解

实现思路:   1,将传进来的图片矩阵用算子进行卷积求和(卷积和取绝对值)   2,用新的矩阵(与原图一样大小)去接收每次的卷积和的值   3,卷积图片所有的像素点后,把新的矩阵数据类型...

Python基于opencv的图像压缩算法实例分析

本文实例讲述了Python基于opencv的图像压缩算法。分享给大家供大家参考,具体如下: 插值方法: CV_INTER_NN - 最近邻插值, CV_INTER_LINEAR - 双线...

使用django-suit为django 1.7 admin后台添加模板

django-grappelli里面使用inline似乎有点儿问题,换一个皮: django-suit是2scoops推荐的第二个admin skin. Supports: Django...

TensorFlow查看输入节点和输出节点名称方式

TensorFlow 定义输入节点名称input_name: with tf.name_scope('input'): bottleneck_input = tf.place...

python实现探测socket和web服务示例

操作系统:linux软件环境:Python 2.7.3 用法:复制代码 代码如下:$ ./MonSocket.py # This is check the URI or Socket...