我正在使用 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/