Python psycopg2时间ou

2024-04-25 16:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个大问题: 运行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)

我尝试为查询添加一些超时,但这没有帮助,因为连接根本没有建立。

有没有办法,当连接无法建立时,我可以立即停止程序?


Tags: theto服务器hostdb软件serverlogging