美文网首页
psycopg2.pool.SimpleConnectionPo

psycopg2.pool.SimpleConnectionPo

作者: lovedrose | 来源:发表于2017-04-11 17:26 被阅读1183次

    <a href= 'http://students.mimuw.edu.pl/~jd334392/project/psycopg2-2.5.2/doc/html/pool.html'>官方文档 点这里</a>

    这里对普通的connection和使用pool的connection进行了10000次查询测试
    普通的代码:

    import psycopg2
    from time import time
    
    t = time()
    n = 10000
    while n:
        n -= 1
        conn = psycopg2.connect(dbname = 'enterprise-test', user = 'lovedrose', password = '', host = '127.0.0.1')
        cur = conn.cursor()
        cur.execute('select * from cmps_key_value')
        cur.fetchall()
    print(time() - t)
    消耗:52.5323209763
    

    使用了连接池的代码

    import psycopg2.pool
    from time import time
    
    t = time()
    n = 10000
    lst = [str(i) for i in range(20)]
    simple_conn_pool = psycopg2.pool.SimpleConnectionPool(5, 200, host = '127.0.0.1',user = 'lovedrose', password = '', dbname = 'enterprise-test')
    while n:
        key = lst.pop(0)
        conn = simple_conn_pool.getconn(key)
        cur = conn.cursor()
        cur.execute('select * from cmps_key_value')
        cur.fetchall()
        n -= 1
        lst.append(key)
    print(time() - t)
    simple_conn_pool.closeall()
    消耗:4.29549717903
    

    快了近12倍

    相关文章

      网友评论

          本文标题:psycopg2.pool.SimpleConnectionPo

          本文链接:https://www.haomeiwen.com/subject/fbsuattx.html