V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
skyduy
V2EX  ›  Python

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

  •  
  •   skyduy ·
    skyduy · 2016-06-09 11:43:32 +08:00 · 3795 次点击
    这是一个创建于 2875 天前的主题,其中的信息可能已经有所发展或是发生改变。

    普通 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 内存,求大神指导...

    1 条回复    2016-06-09 13:22:41 +08:00
    wsy2220
        1
    wsy2220  
       2016-06-09 13:22:41 +08:00 via Android
    检查一下 apache 的 mpm 是不是 prefork ,最好用 event
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1018 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:11 · PVG 04:11 · LAX 13:11 · JFK 16:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.