python - Flask 应用程序停止使用 gunicorn 和主管与数据库通信

标签 python mysql flask gunicorn supervisord

我正在使用 gunicorn 和 supervisor 在 AWS 上运行 Flask 应用程序。该应用程序本身并不太复杂,但我正在从 Python 调用 MySQL 数据库。在 12-24 小时内一切正常,然后我就无法调用数据库了。杀死 supervisor 和 gunicorn 并重新启动应用程序可以解决问题,但我想让它更健壮一些。有什么想法吗?

下面是我在 python 中连接到数据库的方式: db = MySQLdb.connect(user="root", host="localhost", port=3306, db="movie_locations", passwd="mypassword")

我这样查询: db.query(thisquery)

我使用以下命令运行应用程序: sudo supervisord -c simple.conf

这是我的 simple.conf 文件:

[program:myserver]
command=gunicorn myapp:app -w 4 -b 0.0.0.0:80

[supervisord]
logfile=/home/ubuntu/supervisord.log
loglevel=debug
user=root

这些是我在 .log 文件中得到的错误类型:

2014-02-03 07:27:19,320 DEBG fd 6 closed, stopped monitoring <POutputDispatcher at 26772040 for <Subprocess at 26713840 with name m\
yserver in state RUNNING> (stdout)>
2014-02-03 07:27:19,320 DEBG fd 8 closed, stopped monitoring <POutputDispatcher at 26780448 for <Subprocess at 26713840 with name m\
yserver in state RUNNING> (stderr)>
2014-02-03 07:27:19,321 INFO exited: myserver (exit status 1; not expected)
2014-02-03 07:27:19,321 DEBG received SIGCLD indicating a child quit
2014-02-03 07:27:20,327 INFO spawned: 'myserver' with pid 13288
2014-02-03 07:27:20,498 DEBG 'myserver' stderr output:
2014-02-03 07:27:20 [13288] [INFO] Starting gunicorn 18.0

2014-02-03 07:27:20,499 DEBG 'myserver' stderr output:
2014-02-03 07:27:20 [13288] [ERROR] Connection in use: ('0.0.0.0', 80)

2014-02-03 07:27:20,500 DEBG 'myserver' stderr output:
2014-02-03 07:27:20 [13288] [ERROR] Retrying in 1 second.

2014-02-03 07:27:21,501 INFO success: myserver entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2014-02-03 07:27:21,502 DEBG 'myserver' stderr output:
2014-02-03 07:27:21 [13288] [ERROR] Connection in use: ('0.0.0.0', 80)

2014-02-03 07:27:21,502 DEBG 'myserver' stderr output:
2014-02-03 07:27:21 [13288] [ERROR] Retrying in 1 second.

2014-02-03 07:27:22,504 DEBG 'myserver' stderr output:
2014-02-03 07:27:22 [13288] [ERROR] Connection in use: ('0.0.0.0', 80)

非常感谢您的帮助!

最佳答案

我最近遇到了类似的问题。问题是我天真地将数据库游标声明为全局变量。最终,连接超时或以其他方式降级。为了修复它,我将数据库声明放在一个函数中,以便它在超出范围时关闭,并在额外的函数调用时重新建立。

关于python - Flask 应用程序停止使用 gunicorn 和主管与数据库通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21521933/

相关文章:

python - 在列表中查找特定模式

python - 如何一次比较多个列表的数值接近度?

mysql - 从主机访问 Docker MySQL 服务器,无需容器的 IP 地址

python - 无法将 Python 3 Flask 应用程序推送到 Heroku

python - 简单的Python-3程序中的无效语法错误

python - 通过脚本级命令行参数激活 Python 的优化模式

mysql - 删除部分字符串的 SQL 语句

php - 验证问题阻止使用 PHP 更新数据库

python - 使用子路径在 Apache 反向代理后面重定向 Flask 登录

python - 如何通过 python-eve 将图像上传到一些外部存储服务器,例如S3?