首页 > 解决方案 > Apache HTTP Server 和 Flask:避免在一段时间后重新启动 Python 应用程序

问题描述

我有一个使用 Apache HTTP Server 运行的烧瓶服务器。python 程序首先将大量文件加载到内存中(大约需要 10 分钟),然后提供一些 REST API。

在一段时间内一切正常,但在一天左右(或一些不活动?)之后,python 程序关闭。当有新请求进来时,程序会重新启动(所以一段时间后的第一个请求需要 10 分钟)。python程序中创建的缓存自然也是空的。

这是我的/etc/apache2/sites-available/server.conf(我用尖括号替换了我的实际路径):

<VirtualHost *:80>
     # Add machine's IP address (use ifconfig command)
     ServerName <IP here>

     WSGIDaemonProcess myapp python-home=<path>
     WSGIProcessGroup myapp
     WSGIApplicationGroup %{GLOBAL}

     # Give an alias to to start your website url with
     WSGIScriptAlias <some alias> <path to wsgi>
     <Directory <dir>>
             # set permissions as per apache2.conf file
            Options FollowSymLinks
            AllowOverride None
            Require all granted
     </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     LogLevel warn
     CustomLog 
</VirtualHost>

这是我在以下位置找到的 wsgi 文件<path to wsgi>

#! /usr/bin/python3111

import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '<python application root>')
from flask_server import app as application
application.secret_key = 'anything you wish'

我想我错过了一些让 python 程序永远活着的配置行......?

标签: pythonflaskwsgihttpserver

解决方案


推荐阅读