[英]matplotlib is throwing segmentation fault when running on non-gui machine(web server)
[英]Apache web server Segmentation Fault error when connecting to a Postgres Database
我有一个在 apache 服务器上运行的 python flask web 应用程序。 Flask 应用程序只是一个返回我从 postgres 数据库获取的值的函数。
@app.route('/')
def hello_world():
import psycopg2
db_conn_string = ("dbname=" + "xxx"
+ " user=" + "xxx"
+ " host=" + "xxx"
+ " password=" + "xxx")
db_connection = psycopg2.connect(db_conn_string)
cursor = db_connection.cursor()
cursor.execute("SELECT * FROM XXX")
return cursor.fetchall()
当我运行这个应用程序时,我在浏览器上得到了这个:
我检查了/var/log/apache2/error.log
下的日志,错误是:
[Fri Jan 24 10:34:34.676561 2020] [core:notice] [pid 15644:tid 139639322680256] AH00051: child pid 15972 exit signal Segmentation fault (11), possible coredump in /etc/apache2
有趣的是,当我将 return 语句更改为只返回一个 hello world 时,它运行得很好,我没有看到任何错误。 所以我的假设是错误是由于 apache web 应用程序试图连接到 postgres 数据库。 我检查了数据库的凭据,那里没有任何问题。 我不知道如何解决这个问题。
编辑:具有相同代码的测试 python 程序有效,我能够检索数据。
您的视图(即hello_world
函数)需要返回 Flask 支持的内容。 也许可以查看Flask 上的Flask 教程以获取更多示例
最简单的方法就是将其转换为字符串并返回,例如:
import psycopg2
@app.route('/')
def hello_world():
con = psycopg2.connect(db_conn_string)
with con, con.cursor() as cur:
cur.execute("SELECT * from xxx;")
result = cur.fetchall()
return repr(result)
我已经重新安排了您的代码,使其更加传统。 请注意, import
通常位于顶部。 您还希望将连接字符串设为顶部的常量。 使用psycopg2
游标作为上下文管理器也很好地将它们包装在一个事务中,成功时COMMIT
并在异常时执行ROLLBACK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.