1. <tfoot id='OsG0c'></tfoot>
    1. <i id='OsG0c'><tr id='OsG0c'><dt id='OsG0c'><q id='OsG0c'><span id='OsG0c'><b id='OsG0c'><form id='OsG0c'><ins id='OsG0c'></ins><ul id='OsG0c'></ul><sub id='OsG0c'></sub></form><legend id='OsG0c'></legend><bdo id='OsG0c'><pre id='OsG0c'><center id='OsG0c'></center></pre></bdo></b><th id='OsG0c'></th></span></q></dt></tr></i><div id='OsG0c'><tfoot id='OsG0c'></tfoot><dl id='OsG0c'><fieldset id='OsG0c'></fieldset></dl></div>

      <small id='OsG0c'></small><noframes id='OsG0c'>

      <legend id='OsG0c'><style id='OsG0c'><dir id='OsG0c'><q id='OsG0c'></q></dir></style></legend>
      • <bdo id='OsG0c'></bdo><ul id='OsG0c'></ul>

      Python psycopg2 超时

      时间:2023-10-18
        <tbody id='f0xOF'></tbody>

      <small id='f0xOF'></small><noframes id='f0xOF'>

    2. <tfoot id='f0xOF'></tfoot>
        • <bdo id='f0xOF'></bdo><ul id='f0xOF'></ul>
            <legend id='f0xOF'><style id='f0xOF'><dir id='f0xOF'><q id='f0xOF'></q></dir></style></legend>
            • <i id='f0xOF'><tr id='f0xOF'><dt id='f0xOF'><q id='f0xOF'><span id='f0xOF'><b id='f0xOF'><form id='f0xOF'><ins id='f0xOF'></ins><ul id='f0xOF'></ul><sub id='f0xOF'></sub></form><legend id='f0xOF'></legend><bdo id='f0xOF'><pre id='f0xOF'><center id='f0xOF'></center></pre></bdo></b><th id='f0xOF'></th></span></q></dt></tr></i><div id='f0xOF'><tfoot id='f0xOF'></tfoot><dl id='f0xOF'><fieldset id='f0xOF'></fieldset></dl></div>

                本文介绍了Python psycopg2 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个大问题:运行我的 python 软件的服务器的路由器上似乎存在一些硬件问题.只有大约每三次才能成功连接到数据库.因此,psycopg2.connect() 最多可能需要 5 分钟才能获得超时异常.

                I have a huge problem: There seems to be some hardware problems on the router of the server my python software runs on. The connection to the database only is successfull about every third time. So a psycopg2.connect() can take up to 5 minutes before I get an timeout exception.

                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
                

                这就是我正在使用的代码.

                That's the code I'm using.

                # 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)
                

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

                I tried some timeout additions for the query, but that didn't helped, since the connection didn't got established at all.

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

                Is there a way, I can stop the program immediately, when the connection couldn't be established?

                推荐答案

                connect 函数中使用关键字参数语法时,可以使用任何 libpd支持的连接参数.其中有 connect_timeout 秒:

                When using the keyword arguments syntax to the connect function it is possible to use any of the libpd supported connection parameters. Among those there is connect_timeout in seconds:

                db = psycopg2.connect (
                    host=dhost, database=ddatabase,
                    user=duser, password=dpassword,
                    connect_timeout=3
                )
                

                http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS

                http://initd.org/psycopg/docs/module.html

                连接超时引发 OperationalError 例外.

                A connection time out raises an OperationalError exception.

                这篇关于Python psycopg2 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:确保 psycopg2 数据库连接处于活动状态 下一篇:urllib3 connectionpool - 连接池已满,丢弃连接

                相关文章

                <i id='KpMx7'><tr id='KpMx7'><dt id='KpMx7'><q id='KpMx7'><span id='KpMx7'><b id='KpMx7'><form id='KpMx7'><ins id='KpMx7'></ins><ul id='KpMx7'></ul><sub id='KpMx7'></sub></form><legend id='KpMx7'></legend><bdo id='KpMx7'><pre id='KpMx7'><center id='KpMx7'></center></pre></bdo></b><th id='KpMx7'></th></span></q></dt></tr></i><div id='KpMx7'><tfoot id='KpMx7'></tfoot><dl id='KpMx7'><fieldset id='KpMx7'></fieldset></dl></div>
              1. <tfoot id='KpMx7'></tfoot>
                    <bdo id='KpMx7'></bdo><ul id='KpMx7'></ul>

                    <small id='KpMx7'></small><noframes id='KpMx7'>

                    <legend id='KpMx7'><style id='KpMx7'><dir id='KpMx7'><q id='KpMx7'></q></dir></style></legend>