我在

python中编写了一个

Windows服务.如果我从命令提示符运行我的脚本

python runService.py

当我这样做时,服务安装并正确启动.我一直在尝试使用pyinstaller创建一个可执行文件,因为我看到与py2exe相同的问题.当我运行.exe服务安装,但不启动,我得到以下错误

error 1053 the service did not respond to the start or control request in a timely fashion

我看到很多人都有这个问题,但我似乎找不到如何解决这个问题的确切答案.

winservice.py

from os.path import splitext, abspath

from sys import modules, executable

from time import *

import win32serviceutil

import win32service

import win32event

import win32api

class Service(win32serviceutil.ServiceFramework):

_svc_name_ = '_unNamed'

_svc_display_name_ = '_Service Template'

_svc_description_ = '_Description template'

def __init__(self, *args):

win32serviceutil.ServiceFramework.__init__(self, *args)

self.log('init')

self.stop_event = win32event.CreateEvent(None, 0, 0, None)

#logs into the system event log

def log(self, msg):

import servicemanager

servicemanager.LogInfoMsg(str(msg))

def sleep(self, minute):

win32api.Sleep((minute*1000), True)

def SvcDoRun(self):

self.ReportServiceStatus(win32service.SERVICE_START_PENDING)

try:

self.ReportServiceStatus(win32service.SERVICE_RUNNING)

self.log('start')

self.start()

self.log('wait')

win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

self.log('done')

except Exception, x:

self.log('Exception : %s' % x)

self.SvcStop()

def SvcStop(self):

self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

#self.log('stopping')

self.stop()

#self.log('stopped')

win32event.SetEvent(self.stop_event)

self.ReportServiceStatus(win32service.SERVICE_STOPPED)

# to be overridden

def start(self): pass

# to be overridden

def stop(self): pass

def instart(cls, name, description, display_name=None, stay_alive=True):

''' Install and Start (auto) a Service

cls : the class (derived from Service) that implement the Service

name : Service name

display_name : the name displayed in the service manager

decription: the description

stay_alive : Service will stop on logout if False

'''

cls._svc_name_ = name

cls._svc_display_name_ = display_name or name

cls._svc_desciption_ = description

try:

module_path=modules[cls.__module__].__file__

except AttributeError:

module_path=executable

module_file = splitext(abspath(module_path))[0]

cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)

if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True)

try:

win32serviceutil.InstallService(

cls._svc_reg_class_,

cls._svc_name_,

cls._svc_display_name_,

startType = win32service.SERVICE_AUTO_START,

description = cls._svc_desciption_

)

print 'Install ok'

win32serviceutil.StartService(

cls._svc_name_

)

print 'Start ok'

except Exception, x:

print str(x)

UPDATE

我通过使用py2exe解决了这个问题,但同样的改变也可能适用于pyinstaller.我没有时间自己检查.

我不得不删除instart功能.以下是我的winservice.py如何读取.

winservice_py2exe.py

from os.path import splitext, abspath

from sys import modules, executable

from time import *

import win32serviceutil

import win32service

import win32event

import win32api

class Service(win32serviceutil.ServiceFramework):

_svc_name_ = 'actualServiceName' #here is now the name you would input as an arg for instart

_svc_display_name_ = 'actualDisplayName' #arg for instart

_svc_description_ = 'actualDescription'# arg from instart

def __init__(self, *args):

win32serviceutil.ServiceFramework.__init__(self, *args)

self.log('init')

self.stop_event = win32event.CreateEvent(None, 0, 0, None)

#logs into the system event log

def log(self, msg):

import servicemanager

servicemanager.LogInfoMsg(str(msg))

def sleep(self, minute):

win32api.Sleep((minute*1000), True)

def SvcDoRun(self):

self.ReportServiceStatus(win32service.SERVICE_START_PENDING)

try:

self.ReportServiceStatus(win32service.SERVICE_RUNNING)

self.log('start')

self.start()

self.log('wait')

win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

self.log('done')

except Exception, x:

self.log('Exception : %s' % x)

self.SvcStop()

def SvcStop(self):

self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

#self.log('stopping')

self.stop()

#self.log('stopped')

win32event.SetEvent(self.stop_event)

self.ReportServiceStatus(win32service.SERVICE_STOPPED)

# to be overridden

def start(self): pass

# to be overridden

def stop(self): pass

if __name__ == '__main__':

# Note that this code will not be run in the 'frozen' exe-file!!!

win32serviceutil.HandleCommandLine(VidiagService) #added from example included with py2exe

以下是我与py2exe一起使用的setup.py文件.这是从py2exe安装中包含的示例中获取的:

setup.py

from distutils.core import setup

import py2exe

import sys

if len(sys.argv) == 1:

sys.argv.append("py2exe")

sys.argv.append("-q")

class Target:

def __init__(self, **kw):

self.__dict__.update(kw)

# for the versioninfo resources

self.version = "0.5.0"

self.company_name = "No Company"

self.copyright = "no copyright"

self.name = "py2exe sample files"

myservice = Target(

# used for the versioninfo resource

description = "A sample Windows NT service",

# what to build. For a service, the module name (not the

# filename) must be specified!

modules = ["winservice_py2exe"]

)

setup(

options = {"py2exe": {"typelibs":

# typelib for WMI

[('{565783C6-CB41-11D1-8B02-00600806D9B6}', 0, 1, 2)],

# create a compressed zip archive

"compressed": 1,

"optimize": 2}},

# The lib directory contains everything except the executables and the python dll.

# Can include a subdirectory name.

zipfile = "lib/shared.zip",

service = [myservice]

)

一旦您创建了exe,您可以使用以下命令从命令安装服务

winservice_py2exe.exe -install

然后启动您可以使用的服务:

net start aTest

或从Windows服务管理器.所有其他Windows命令行功能现在可以在服务以及Windows服务管理器上工作.

python 打包成exe 1053_Python Windows服务pyinstaller可执行文件错误1053相关推荐

  1. python 打包成exe 1053_Python程序打包成exe的一些坑

    今天写了一个项目,Python项目,需要在win7上跑起来,我想,这不是简单的不行么,直接上Pyinstaller不就完了? 但是后来,我发觉我真是too young too simple. 为什么这 ...

  2. Python打包成exe文件_详细操作

    Python打包成exe文件 前言 一.安装pyinstaller 1.1 安装pyinstaller,使用安装命令: 1.2 如果遇到需要更新版本请输入: 1.3 检查是否正确安装 1.4 稍等,水 ...

  3. 把python语言翻译出来_Python语言实现翻译小工具(Python打包成exe文件)

    本文主要向大家介绍了Python语言实现翻译小工具(Python打包成exe文件),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助. 1.环境 windows10 python3.5 ...

  4. python打包成.exe程序

    一.需求 有些时候,我们想做个带图形化界面的小工具用于pc端,使用MFC当然可以,java也有何不可,那么使用python呢?是否也可以把带有图形化界面的python程序打包成.exe程序?答案是肯定 ...

  5. cmd python封装成exe_别再问我怎么Python打包成exe了!

    也许我们不一定是专业的程序员,但是我们仍然可以通过代码提高我们的效率,尽量少加班,多陪陪媳妇(如果有).再不行,让代码替我们干着重复的工作,我们有节省出来的时间打游戏不好嘛,是吧,哈哈哈. 但是呢,我 ...

  6. Python打包成exe,pyc

    D:\mypython\path\ C:\Python27\Scripts\pyinstaller.exe -w mypython.py # Python打包成exe D:\mypython\path ...

  7. python打包成.exe文件时出现“系统找不到指定路径”

    python打包成.exe文件时出现"系统找不到指定路径" 我在一开始写工程时就想到最后打包的时候可能会出现文件位置会发生移动,所以并没有使用绝对路径,而都是以相对路径写的程序. ...

  8. Python打包成exe,文件太大问题解决办法

    Python打包成exe,文件太大问题解决办法 原因 解决办法 具体步骤 情况一:初次打包 情况二:再次打包 原因 由于使用pyinstaller打包.py文件时,会把很多已安装的无关库同时打包进去, ...

  9. python如何将图片打包进exe里_史上最详细的Python打包成exe文件教程

    打包成exe文件可以让python代码在没有python环境的条件下,依然能够运行,实在是码农们写追女朋友表白.情人节浪漫的必需品! 1.使用豆瓣镜像源下载: pyinstaller 有需要了解如何使 ...

最新文章

  1. vue与react组件的思考
  2. Scrapy基本用法
  3. MOSSE目标跟踪算法的理解
  4. vue 父组件与子组件之间的传值(主动传值)
  5. 数据结构 - 赫夫曼树
  6. 低介电常数微波介质陶瓷基覆铜板的研究
  7. 学习记录之显示屏语言模块确定,星瞳学习
  8. svn图标没有显示的解决办法
  9. 华为“不造车”的承诺,快到期了
  10. Android获取手机MAC地址
  11. 银行大数据应用场景:客户画像如何做?
  12. win10注册ocx控件的步骤(包含错误处理方法0x80040200)
  13. MongoDB——ISODate日期类型
  14. RestTemplateConfig
  15. 深入剖析原理!Android面试你必须要知道的那些知识,吐血整理
  16. curl命令学习使用小结
  17. HTML5表单:工具箱中的可靠工具
  18. 1734: 炮兵阵地
  19. 计算机保护系统软件,雨过天晴电脑保护系统专业版
  20. 新型机房建设的优点有哪些?

热门文章

  1. 真菌和细菌高通量测序引物选择
  2. laravel图片和文件的上传
  3. WordPress上传图片提示:服务器无法处理图像
  4. (2021最新版)软件测试面试题!全背下来,月薪10K!
  5. 端到端语音识别模型LAS(listen-attention-spell)
  6. C++STL标准库学习笔记(六)multimap
  7. 洛谷 1966 loj 2069 火柴排队 题解
  8. python用pip安装numpy完整命令_python – pip无法安装numpy错误代码1
  9. 怎么退还保证金?湖北智禾教育为你解忧
  10. 已知直线过两点,和线外一点,求直线和垂足及垂距