ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-psycopg2.InterfaceError:连接已关闭/ pgr_astar

2019-11-22 12:05:34  阅读:214  来源: 互联网

标签:a-star postgresql psycopg2 python pgrouting


我正在使用psycopg2从python访问postgresql数据库.当我尝试为最短的路径运行填充函数“ pgr_astar”时,出现错误

cur = db.cursor()
psycopg2.InterfaceError: connection already closed

基本上,发生的事情是:当pgr_astar在两点之间找不到路径时,它将导致数据库崩溃,并关闭与数据库的连接.

如何避免这种情况?

我试图用try / except隔离功能并创建自己的连接,所以如果关闭就没问题:

conn = psycopg2.connect(...)
try:
    result = getRecords(conn,query_pgr_astar)
    return float(result[0]['sum'])
except:
    return 500000000.0
conn.close()

但是在这种情况下,我收到了

cur.execute(query)
psycopg2.DatabaseError: SSL SYSCALL error: EOF detected

如何管理不可能的路径?

(我对pgdb和旧的shortest_path_astar函数做了同样的操作(现在不再可用),我没有问题)

PostgreSQL日志是:

2013-11-27 15:54:35 CET LOG:  terminating any other active server processes
2013-11-27 15:54:35 CET WARNING:  terminating connection because of crash of another server process
2013-11-27 15:54:35 CET DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2013-11-27 15:54:35 CET HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2013-11-27 15:54:35 CET WARNING:  terminating connection because of crash of another server process
2013-11-27 15:54:35 CET DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2013-11-27 15:54:35 CET HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2013-11-27 15:54:35 CET LOG:  all server processes terminated; reinitializing
2013-11-27 15:54:35 CET LOG:  database system was interrupted; last known up at 2013-11-27 15:42:01 CET
2013-11-27 15:54:35 CET LOG:  database system was not properly shut down; automatic recovery in progress
2013-11-27 15:54:35 CET LOG:  record with zero length at 65/80875D60
2013-11-27 15:54:35 CET LOG:  redo is not required
2013-11-27 15:54:35 CET LOG:  autovacuum launcher started
2013-11-27 15:54:35 CET LOG:  database system is ready to accept connections

我做的查询是:

SELECT SUM(t2.length) FROM (SELECT id2 FROM pgr_astar('SELECT edge_id AS id, vertex_id1 AS source, vertex_id2 AS target, ' || '(CASE WHEN door = ''S'' THEN -1.0  ELSE  (length * (CASE network WHEN ''0'' THEN 1.0  WHEN ''10'' THEN 1.2  WHEN ''20'' THEN 1.5  WHEN ''30'' THEN 2.0  ELSE 1.0  END) *  (CASE type WHEN ''9.1'' THEN CASE WHEN level1 < level2 THEN 3.0 ELSE 2.0 END  WHEN ''9.2'' THEN CASE WHEN level1 < level2 THEN 15.0 ELSE 12.0 END  ELSE 1.0  END) +  (CASE type WHEN ''9.3'' THEN 40.0  ELSE 0.0  END)  )  END)::float8 AS cost, ' || '(CASE WHEN door_rev = ''S'' THEN -1.0  ELSE  (length * (CASE network WHEN ''0'' THEN 1.0  WHEN ''10'' THEN 1.2  WHEN ''20'' THEN 1.5  WHEN ''30'' THEN 2.0  ELSE 1.0  END) *  (CASE type WHEN ''9.1'' THEN CASE WHEN level1 < level2 THEN 3.0 ELSE 2.0 END  WHEN ''9.2'' THEN CASE WHEN level1 < level2 THEN 15.0 ELSE 12.0 END  ELSE 1.0  END) +  (CASE type WHEN ''9.3'' THEN 40.0  ELSE 0.0  END)  )  END )::float8 AS reverse_cost, ' || 'x1, y1, x2, y2 FROM edges', " + str(destination_route) + ", " + str(origin_route) + ", TRUE, TRUE)) as t1, edges as t2 where t1.id2 = t2.edge_id

door =“ S”表示门已关闭,在这种情况下,值为-1.

解决方法:

我假设您可以在python中检查连接状态(我不使用python),以便您可以恢复并做一些明智的事情,例如重新连接.在重新连接之前,您可能需要休眠几秒钟以使数据库从崩溃中恢复.

关于灌浆问题,我希望看到一个简单的测试用例,它可以在pgadmin或psql shell中重现此问题,并作为错误提交,因此我可以对其进行调查.我们的代码永远都不会使服务器崩溃,但是有时它会使服务器崩溃,因此我们需要修复这些问题.

标签:a-star,postgresql,psycopg2,python,pgrouting
来源: https://codeday.me/bug/20191122/2059797.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有