Flask WSGI 应用程序内存不足

发布于 2025-01-07 04:52:22 字数 2019 浏览 0 评论 0原文

我有一个相当简单的 python Flask 应用程序,在 Apache2 下作为 WSGI 进程运行。 该应用程序有一个侦听器,通过使用 SQLAlchemy 从数据库检索几行数据并将其作为 JSON 发送回。

对于 MySql 连接,我确实有一个正在重用的全局引擎。

使用 JMeter 生成一些负载,Apache2 进程每 5 秒将 RAM 使用量增加 0.5% 个单位,并且很快就会耗尽 RAM。如果停止 JMeter 生成负载,内存不会被清除。

httpd.conf

<VirtualHost *:80>
ServerName xxxxxxxxxx.com

<Directory /var/www/xxxxxxxxxx>
    Order allow,deny
Allow from all
</Directory>

WSGIDaemonProcess BiddingPractice user=www-data group=www-data threads=5
WSGIScriptAlias /flask /var/www/xxxxxxxx/xxxxxxxxxxx.wsgi

<Directory /var/www/BiddingPractice>
    WSGIProcessGroup BiddingPractice
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

WSGI 文件

import sys
sys.path.insert(0, '/home/stefan/Code/xxxxxx')
from BiddingPractice import app as application

_init.py_

    # -*- coding: utf-8 *-*
from flask import Flask
from sqlalchemy import *

app = Flask(__name__)
db = create_engine('mysql://root:xxxxxx@localhost/xxxxxxx')

import BiddingPractice.main

ma​​in.py

    # -*- coding: utf-8 *-*
from flask import render_template
from flask import request
from flask import make_response
from flask import jsonify
from BiddingPractice import app, db
from Data.users import getUsers
import random

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('temp.html', name=name)

@app.route('/getData')
def getData():

    un, psw, gids = getUsers()

    random.shuffle(un)
    random.shuffle(gids)

    usernames = ','.join(map(str, un))
    passwords = ','.join(map(str, psw))
    guids = ','.join(map(str, gids))

    return jsonify(usernames=usernames, passwords=passwords, guids=guids)

任何人都可以告诉我是否遗漏了某些内容或给我一些有关如何解决内存使用问题的提示,例如我如何查看 Apache2 进程的填充情况?

感谢您的帮助!

I have a a fairly simple python Flask application running as a WSGI process under Apache2.
The application has a listener by using SQLAlchemy to retrieve a few lines of data from the DB and sending it back as a JSON

For the MySql connection I do have one global engine being reused.

Using JMeter to generate some load, the Apache2 process increase RAM usage by 0.5% units per every 5 second and very quickly running out of RAM. If stopping the JMeter generating the load the memory does not get cleared out.

httpd.conf

<VirtualHost *:80>
ServerName xxxxxxxxxx.com

<Directory /var/www/xxxxxxxxxx>
    Order allow,deny
Allow from all
</Directory>

WSGIDaemonProcess BiddingPractice user=www-data group=www-data threads=5
WSGIScriptAlias /flask /var/www/xxxxxxxx/xxxxxxxxxxx.wsgi

<Directory /var/www/BiddingPractice>
    WSGIProcessGroup BiddingPractice
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

WSGI File

import sys
sys.path.insert(0, '/home/stefan/Code/xxxxxx')
from BiddingPractice import app as application

_init.py_

    # -*- coding: utf-8 *-*
from flask import Flask
from sqlalchemy import *

app = Flask(__name__)
db = create_engine('mysql://root:xxxxxx@localhost/xxxxxxx')

import BiddingPractice.main

main.py

    # -*- coding: utf-8 *-*
from flask import render_template
from flask import request
from flask import make_response
from flask import jsonify
from BiddingPractice import app, db
from Data.users import getUsers
import random

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('temp.html', name=name)

@app.route('/getData')
def getData():

    un, psw, gids = getUsers()

    random.shuffle(un)
    random.shuffle(gids)

    usernames = ','.join(map(str, un))
    passwords = ','.join(map(str, psw))
    guids = ','.join(map(str, gids))

    return jsonify(usernames=usernames, passwords=passwords, guids=guids)

Anyone can tell if I have missed something or give me some tips on how to trouble shoot the memory usage, for example how could I see what is filling up the Apache2 process?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2025-01-14 04:52:22

使用 Flask-SQLAlchemy。它负责正确处理连接和清理事物。

除此之外,它还为您提供了很多糖分,例如 Modelclass.query 而不是 db.session.query(Modelclass)

Use Flask-SQLAlchemy. It takes care about proper handling of the connection and cleaning up things.

Besides that, it gives you lots of sugar such as Modelclass.query instead of db.session.query(Modelclass)

披肩女神 2025-01-14 04:52:22

您没有在请求结束时删除 SQLAlchemy 会话对象。这应该在应用程序的 teardown_request 处理程序中完成(请参阅 Flask 文档中的示例)。 Flask-SQLAlchemy 会为您完成此操作,但目前不需要切换到 Flask-SQLAlchemy。

You did not remove SQLAlchemy session object at the end of request. This should be done in teardown_request handler of your application (see example in Flask documentation). Flask-SQLAlchemy does this for you, but switching to Flask-SQLAlchemy is not required by far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文