(connstr, keepalive = True,
                     tcp_keepidle = 4 * 60,     # 7200
                     tcp_keepcnt = 4,           # 9
                     tcp_keepintvl = 15)
return db
Create a db connection with connect_timeout and TCP keepalive.
    
    Default connect_timeout is 15, to change put it directly into dsn.

    The extra tcp_* options are Linux-specific, see `man 7 tcp` for details.
Create a db connection with connect_timeout and TCP keepalive.
    
    Default connect_timeout is 15, to change put it directly into dsn.
Create a db connection with connect_timeout and TCP keepalive . Default connect_timeout is 15 to change put it directly into dsn .
def connect_database(connstr, keepalive = True,
                     tcp_keepidle = 4 * 60,     # 7200
                     tcp_keepcnt = 4,           # 9
                     tcp_keepintvl = 15):       # 75
    """Create a db connection with connect_timeout and TCP keepalive.
    
    Default connect_timeout is 15, to change put it directly into dsn.

    The extra tcp_* options are Linux-specific, see `man 7 tcp` for details.
    """

    # allow override
    if connstr.find("connect_timeout") < 0:
        connstr += " connect_timeout=15"

    # create connection
    db = _CompatConnection(connstr)
    curs = db.cursor()

    # tune keepalive
    fd = hasattr(db, 'fileno') and db.fileno() or curs.fileno()
    set_tcp_keepalive(fd, keepalive, tcp_keepidle, tcp_keepcnt, tcp_keepintvl)

    # fill .server_version on older psycopg
    if not hasattr(db, 'server_version'):
        iso = db.isolation_level
        db.set_isolation_level(0)
        curs.execute('show server_version_num')
        db.server_version = int(curs.fetchone()[0])
        db.set_isolation_level(iso)

    return db
def connect_database ( connstr , keepalive = True , tcp_keepidle = 4 * 60 , # 7200 tcp_keepcnt = 4 , # 9 tcp_keepintvl = 15 ) : # 75 # allow override if connstr . find ( "connect_timeout" ) < 0 : connstr += " connect_timeout=15" # create connection db = _CompatConnection ( connstr ) curs = db . cursor ( ) # tune keepalive fd = hasattr ( db , 'fileno' ) and db . fileno ( ) or curs . fileno ( ) set_tcp_keepalive ( fd , keepalive , tcp_keepidle , tcp_keepcnt , tcp_keepintvl ) # fill .server_version on older psycopg if not hasattr ( db , 'server_version' ) : iso = db . isolation_level db . set_isolation_level ( 0 ) curs . execute ( 'show server_version_num' ) db . server_version = int ( curs . fetchone ( ) [ 0 ] ) db . set_isolation_level ( iso ) return db