Python Flask as Windows Service

限于喜欢 提交于 2021-02-05 18:54:25

问题


I am trying to get a Flask app to run as a Service in Windows. I have already tried to implement a solution as suggested here and here without success.

I have a simple folder with just two files:

Project
 |
 +-- myapp.py   
 +-- win32_service.py

Inside myapp.py is a simple Flask app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

And the service skeleton win32_service.py:

import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import servicemanager
import socket
import time
import logging
import os
import sys

sys.path.append(os.path.dirname(__name__))

from myapp import app

logging.basicConfig(
    filename = r'c:\tmp\flask-service.log',
    level = logging.DEBUG, 
    format = '[flaskapp] %(levelname)-7.7s %(message)s'
)

class HelloFlaskSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "FlaskApp"
    _svc_display_name_ = "FlaskApp Service"

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

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        logging.info('Stopped service ...')
        self.stop_requested = True

    def SvcDoRun(self):
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_,'')
        )

        self.main()

    def main(self):
        app.run(host="127.0.0.1", port=8000)

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

I then compiled this to an exe file via pyinstaller using this command:

pyinstaller --onefile --hidden-import win32timezone win32_service.py

I get the compiled exe successfully built. I then proceed to register the service (open cmd with admin privileges):

>>> win32_service.exe install
> Installing service FlaskApp
> Service installed

And I try to start it:

>>> win32_service.exe start
> Starting service FlaskApp

But then nothing happens (no errors). Also if I try to start it from the Task Manager it changes the Status to Starting and then to Stopped.

These are the modules installed in the virtualenv:

altgraph==0.16.1
Click==7.0
Flask==1.0.2
future==0.17.1
itsdangerous==1.1.0
Jinja2==2.10.1
macholib==1.11
MarkupSafe==1.1.1
pefile==2018.8.8
PyInstaller==3.4
pyodbc==4.0.26
pywin32==224
pywin32-ctypes==0.2.0
Werkzeug==0.15.2

System specs:

Python - 3.6.5 
OS     - Windows 10

What I am missing here? Any help is appreciated.

EDIT

Windows EventViewer shows an error:

The description for Event ID 3 from source FlaskApp cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

Traceback (most recent call last):
  File "lib\site-packages\win32\lib\win32serviceutil.py", line 839, in SvcRun
  File "win32_service.py", line 47, in SvcDoRun
  File "win32_service.py", line 50, in main
  File "lib\site-packages\flask\app.py", line 938, in run
  File "lib\site-packages\flask\cli.py", line 629, in show_server_banner
  File "lib\site-packages\click\utils.py", line 260, in echo
SystemError: <built-in method replace of str object at 0x000001E36AD465D0> returned a result with an error set

EDIT 2

If I use a single spec file, some modules are not found by the hidden import (this is the output from pyinstaller:

4972 INFO: Analyzing hidden import 'ClickFlask'
4973 ERROR: Hidden import 'ClickFlask' not found
4974 INFO: Analyzing hidden import 'future'
4981 INFO: Analyzing hidden import 'itsdangerous'
5029 INFO: Analyzing hidden import 'Jinja2'
5030 ERROR: Hidden import 'Jinja2' not found
5030 INFO: Analyzing hidden import 'MarkupSafe'
5032 ERROR: Hidden import 'MarkupSafe' not found
5033 INFO: Analyzing hidden import 'pyodbc'
5034 INFO: Analyzing hidden import 'pywin32'
5035 ERROR: Hidden import 'pywin32' not found
5035 INFO: Analyzing hidden import 'pywin32-ctypes'
5036 ERROR: Hidden import 'pywin32-ctypes' not found

Could it have to do with this? Why are some modules found and others don't? I am using a virtualenv.


回答1:


According to a Reddit post, Adding all the libraries to hiddenimports should fix your problem, I tried it myself and it did work!

So, create a file in your project's directory, named win32_service.spec with the following content

# -*- mode: python -*-

block_cipher = None


a = Analysis(['win32_service.py'],
             pathex=['C:\\Users\\Win7\\Desktop\\FaaS'],
             binaries=[],
             datas=[],
             hiddenimports=['win32timezone',
                            'altgraph',
                            'Click'
                            'Flask',
                            'future',
                            'itsdangerous',
                            'Jinja2',
                            'macholib',
                            'MarkupSafe',
                            'pefile',
                            'PyInstaller',
                            'pyodbc',
                            'pywin32',
                            'pywin32-ctypes',
                            'Werkzeug',],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='win32_service',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

Don't forget to change pathex variable

Then instead of pyinstaller --onefile --hidden-import win32timezone win32_service.py use the following command: pyinstaller --onefile win32_service.spec




回答2:


I looked further into pyinstaller github repo and solved this issue.

It seems that pyinstaller has some conflicts with Windows 10, but this issue was the key to my problem. Althoug the module producing the error was not the same.

I managed to solve it by adding a SystemError exception at lib\site-packages\click\utils.py, line 260 in the echo function.

So I change this:

if message:
   file.write(message)

To this:

if message:
    try:
        file.write(message)
    except SystemError:
        pass

Rebuilt the exe using:

pyinstaller --onefile --hidden-import win32timezone win32_service.py

Installed the service, and then it started correctly.



来源:https://stackoverflow.com/questions/55677165/python-flask-as-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!