Python服务失败,错误1053但适用于调试模式

时间:2016-05-04 21:35:17

标签: python py2exe pywin32

我正在尝试从我的代码创建Python服务.exe。 我一直坚持运行它。

我有两个重要文件:服务代码:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
from mailbox.fetcher import main_fetching
from disc.my_logger import set_up_logging

logging = set_up_logging("Fetcher")


class FetcherSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "MailMan-FetchingService"
    _svc_display_name_ = "MailMan FetchingService"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.stop_event = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)
        self.stop_requested = False

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.stop_event)
        logging.info('Stopping fetching ...')
        self.stop_requested = True

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_,'')
        )
        self.main()

    def main(self):
        logging.info(' ** Starting fetching mails service ** ')
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        while not self.stop_requested:
            main_fetching()

py2exe设置代码:

setup(console=[{"script": "fetching_service.py",
                "icon_resources": [(1, "../mailman.ico")],
                "dest_base": "fetching_service"}],
      data_files=[('', ['../cacert.pem', '../trusted-certs.crt'])],
      options={"py2exe": {
          "includes": ["win32serviceutil", "win32service", "win32event"],
          'bundle_files': 0, "optimize": 2,
          'dist_dir': 'fetcher_service'}})

服务安装正确:

$ fetcher_service/fetching_service.exe --startup=delayed install
Installing service MailMan-FetchingService
Changing service configuration
Service updated

但是在开始时:

$ fetcher_service/fetching_service.exe start
Starting service MailMan-FetchingService
Error starting service: Usługa nie odpowiada na sygnał uruchomienia lub sygnał sterujący w oczekiwanym czasie.

(翻译成英文:服务没有及时响应启动或控制请求) 我尝试设置特定的用户,但它没有帮助... 此外:运行后错误发生,没有延迟

忘记添加:服务在调试模式下完美运行。

3 个答案:

答案 0 :(得分:4)

此问题可能是由于pywin32安装失败引起的。使用Sys Internals出色的procmon我发现pythonservice.exe无法加载pywintypes34.dll。运行c:\ python34 \ scripts \ pywin32_postinstall.py修复了问题。注意如果您已经将pywin32安装为pypiwin32,因为您使用了pip,那么您需要修复C:\ Python34 \ Lib \ site-packages \ pypiwin32_system32 dir是C:\ Python34 \ Lib \ site-packages \ pywin32_system32,脚本可以工作。

答案 1 :(得分:2)

您必须通知Windows您的服务实际正在运行。在SvcDoRun内,请致电

self.ReportServiceStatus(win32service.SERVICE_START_PENDING)

并且一旦您的服务实际开始(例如,一旦您到达主要,或甚至直接到达)

self.ReportServiceStatus(win32service.SERVICE_RUNNING)

将您的服务标记为成功启动

答案 2 :(得分:0)

我已经解决了这个问题。 我已经使用了phihag的答案并自己更改了setup.py:

setup(service=[{'modules':'fetching_service','cmdline_style':'pywin32','description':'your service description',
    # console=[{"script": "fetching_service.py",
                 "icon_resources": [(1, "../mailman.ico")],
                 "dest_base": "fetching_service"}],
      data_files=[('', ['../cacert.pem', '../trusted-certs.crt'])],
      options={"py2exe": {
          "includes": ["win32serviceutil", "win32service", "win32event"],
          'bundle_files': 0, "optimize": 2,
          'dist_dir': 'fetcher_service'}})