gpt4 book ai didi

作为 Windows 服务的 Python Flask

转载 作者:太空狗 更新时间:2023-10-29 17:07:51 26 4
gpt4 key购买 nike

我正在尝试让 Flask 应用程序在 Windows 中作为服务运行。我已经尝试按照建议实现解决方案 herehere没有成功。

我有一个只有两个文件的简单文件夹:

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

myapp.py 内部是一个简单的 Flask 应用程序:

from flask import Flask

app = Flask(__name__)

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

和服务骨架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)

然后我使用以下命令通过 pyinstaller 将其编译为 exe 文件:

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

我成功构建了已编译的 exe。然后我继续注册该服务(以管理员权限打开 cmd):

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

然后我尝试启动它:

>>> win32_service.exe start
> Starting service FlaskApp

但随后什么也没有发生(没有错误)。此外,如果我尝试从任务管理器启动它,它会将状态更改为 Starting,然后更改为 Stopped

这些是安装在 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

系统规范:

Python - 3.6.5 
OS - Windows 10

我在这里缺少什么?任何帮助表示赞赏。

编辑

Windows 事件查看器显示错误:

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

编辑 2

如果我使用单个 spec 文件,隐藏导入找不到某些模块(这是 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

会不会跟这个有关?为什么有的模块能找到,有的却找不到?我正在使用 virtualenv。

最佳答案

根据 Reddit post ,将所有库添加到 hiddenimports 应该可以解决您的问题,我自己尝试过并且确实有效!

因此,在您的项目目录中创建一个名为 win32_service.spec 的文件,内容如下

# -*- 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 )

不要忘记更改 pathex 变量

然后代替 pyinstaller --onefile --hidden-import win32timezone win32_service.py 使用以下命令:pyinstaller --onefile win32_service.spec

关于作为 Windows 服务的 Python Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com