我在AWS上使用gunicorn和主管运行Flask应用程序。应用程序本身并不太复杂,但我正在从Python调用MySQL数据库。一切都运行良好12-24小时,然后我不能打电话到数据库。杀死主管和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)
非常感谢您的帮助!
答案 0 :(得分:0)
我最近遇到过类似的问题。问题是我天真地将数据库游标声明为全局变量。最终,连接超时或以其他方式降级。为了解决这个问题,我将数据库声明放在一个函数中,以便它在超出范围时关闭,并在另外的函数调用时重新建立。