在 Apache2 上部署 Flask 应用,内存不断消耗的问题,求大神解释指导。

2016-06-09 11:43:32 +08:00
 skyduy

普通 Flask 应用,如果直接调试运行的话(就是使用 python app.py 运行的时候),仅会在第一次访问网页(第一次调用各种函数、包)的时候消耗部分内存(在这里是 50M )。但是部署在 Apache2 上之后,每次访问都会吃掉我 50M 内存。这是什么原因?

下面是 apache2 的 conf 代码:

<VirtualHost *:80>
                ServerName s.example.com

                WSGIScriptAlias / /var/www/my_app/index.wsgi
                <Directory /var/www/my_app>
                        Order Deny,Allow
                        Deny from all
                        Allow from ....
                        Require all granted
                </Directory>

                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

下面是 index.wsgi 代码( virtualenv 创建了 /var/www/my_app/env 目录并安装了依赖包):

#!/usr/bin/python
import logging
import os, sys
sys.path.insert(0, "/var/www/my_app")

activate_this = os.path.join('/var/www/my_app/env', 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
logging.basicConfig(stream=sys.stderr)

from app import create_app
application = create_app()

还是那句话,在我调试运行的时候,第一次加载各种包的时候吃掉我 50M 内存,之后正常,但是部署到 apache2 中之后,每次访问都要吃掉我 50M 内存,求大神指导...

3810 次点击
所在节点    Python
1 条回复
wsy2220
2016-06-09 13:22:41 +08:00
检查一下 apache 的 mpm 是不是 prefork ,最好用 event

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/284576

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX