简体   繁体   English

使用py2exe创建Windows服务

[英]Using py2exe to create windows service

I need to run my python app as windows service. 我需要将python应用程序作为Windows服务运行。 I'm able to do that using commands, 我可以使用命令来做到这一点,
python fservice.py install
python fservice.py start

Now, i want to create exe for my app using py2exe. 现在,我想使用py2exe为我的应用程序创建exe。 I've followed code from this question: link 我已遵循此问题的代码: 链接

setup.py 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.0.1"
        self.company_name = "flotomate"
        self.copyright = "no copyright"
        self.name = "flotomate"

myservice = Target(
     # used for the versioninfo resource
     description = "flotomate",
     # what to build.  For a service, the module name (not the
     # filename) must be specified!
     modules = ["fservice"]
     )

setup(
     service = [myservice]
    )


fservice.py fservice.py

import sys

import servicemanager
import win32serviceutil
import win32service
import win32event
import win32api
from pagent import app


class fservice(win32serviceutil.ServiceFramework):
    _svc_name_ = 'flotomate' #here is now the name you would input as an arg for instart
    _svc_display_name_ = 'flotomate' #arg for instart
    _svc_description_ = 'flotomate'# 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:
            self.SvcStop()

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.stop()
        win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def start(self):
        app.run(host='0.0.0.0',port=4999)

    # to be overridden
    def stop(self):
        pass


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(fservice)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(fservice)

I'm creating exe using command, 我正在使用命令创建exe,
python setup.py py2exe

but, when i try to install the service using 但是,当我尝试使用安装服务时
fservice.exe --install

I get this error 我得到这个错误

Traceback (most recent call last):
  File "boot_service.py", line 37, in <module>
AttributeError: 'module' object has no attribute 'Initialize


boot_service.py of py2exe py2exe的boot_service.py
I'm using Python 2.7.6 and py2exe-0.6.9 我正在使用Python 2.7.6和py2exe-0.6.9

I encountered the same problem. 我遇到了同样的问题。 I don't know whether you found the solution yet 我不知道你是否找到解决方案

In my case, the reason would be that servicemanager is not included in the compiled package. 就我而言,原因是servicemanager未包含在已编译的软件包中。 It seems the installed servicemanager library in python issues conflict. 看来在python中安装的servicemanager库存在冲突。

To solve this problem, I uninstall servicemanager if it is unused or manually copy servicemanager.pyd to folder dist and servicemager.pyc to dist\\library.zip . 为了解决此问题,如果未使用,请卸载servicemanager或将servicemanager.pyd手动复制到dist文件夹,将servicemager.pyc手动复制到dist \\ library.zip If there is a folder named servicemanager in dist\\library.zip , just delete it. 如果dist \\ library.zip中有一个名为servicemanager的文件夹,则将其删除。

If you already had a better solution, please share it ^^ 如果您已有更好的解决方案,请分享^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM