簡體   English   中英

使用py2exe創建Windows服務

[英]Using py2exe to create windows service

我需要將python應用程序作為Windows服務運行。 我可以使用命令來做到這一點,
python fservice.py install
python fservice.py start

現在,我想使用py2exe為我的應用程序創建exe。 我已遵循此問題的代碼: 鏈接

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

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)

我正在使用命令創建exe,
python setup.py py2exe

但是,當我嘗試使用安裝服務時
fservice.exe --install

我得到這個錯誤

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


py2exe的boot_service.py
我正在使用Python 2.7.6和py2exe-0.6.9

我遇到了同樣的問題。 我不知道你是否找到解決方案

就我而言,原因是servicemanager未包含在已編譯的軟件包中。 看來在python中安裝的servicemanager庫存在沖突。

為了解決此問題,如果未使用,請卸載servicemanager或將servicemanager.pyd手動復制到dist文件夾,將servicemager.pyc手動復制到dist \\ library.zip 如果dist \\ library.zip中有一個名為servicemanager的文件夾,則將其刪除。

如果您已有更好的解決方案,請分享^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM