我有个大问题: 运行python软件的服务器的路由器上似乎有一些硬件问题。与数据库的连接大约每三次成功一次。因此,psycopg2.connect()可能需要5分钟才能收到超时异常。
2014-12-23 15:03:12,461 - ERROR - could not connect to server: Connection timed out
Is the server running on host "172.20.19.1" and accepting
这就是我正在使用的代码。
# Connection to the DB
try:
db = psycopg2.connect(host=dhost, database=ddatabase,
user=duser, password=dpassword)
cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
except psycopg2.DatabaseError, err:
print(str(err))
logging.error(str(err))
logging.info('program terminated')
sys.exit(1)
我尝试为查询添加一些超时,但这没有帮助,因为连接根本没有建立。
有没有办法,当连接无法建立时,我可以立即停止程序?
对
connect
函数使用关键字参数语法时,可以使用任何受libpd
支持的连接参数。其中有connect_timeout
秒:http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS
http://initd.org/psycopg/docs/module.html
连接超时引发^{} 异常。
相关问题 更多 >
编程相关推荐