V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dikT
V2EX  ›  分享发现

屌屌的 Postgresql 连接池

  •  
  •   dikT · 2017-03-31 13:48:36 +08:00 · 5798 次点击
    这是一个创建于 2583 天前的主题,其中的信息可能已经有所发展或是发生改变。

    屌屌的 Postgresql 连接池

    原文是这么描述的

    class psycopg2.pool.AbstractConnectionPool(minconn, maxconn, *args, **kwargs)
    	Base class implementing generic key-based pooling code.
    	
    	New minconn connections are created automatically. The pool will support a maximum of about maxconn connections. *args and **kwargsare passed to the connect() function.
    	
    	The following methods are expected to be implemented by subclasses:
    	
    	getconn(key=None)
    	Get a free connection and assign it to key if not None.
    	
    	putconn(conn, key=None, close=False)
    	Put away a connection.
    	
    	If close is True, discard the connection from the pool.
    	
    	closeall()
    	Close all the connections handled by the pool.
    	
    	Note that all the connections are closed, including ones eventually in use by the application.
    

    大致意思是说在创建这个pool对象时,会自动创建参数minconn个数的连接池.并且最终最多能支持maximum这么多个链接

    然后这个pool提供getconn,putconn,closeall三个方法

    getconn用于获取一个链接, 可选参数key,传入获取对应的链接

    putconn回收一个链接, 可选参数key,与 get 与之相对

    closeall关闭所有链接

    试一试

    普通的 connect, 10000 次查询

    import database
    from time import time
    t = time()
    n = 10000
    db = database.PSQL()
    while n:
        db.get_conn()
        data = db.query(table="vshop_order",
                          columns=["id", "order_no", "state"],
                          order_by="-id",
                          limit=1)
        n -= 1
    print(time() - t) 
    $: 138.07099604606628
    

    使用连接池

    import database
    from time import time
    db = database.PSQL()
    lst = [str(i) for i in range(20)]
    t = time()
    n = 10000
    while n:
        key = lst.pop(0)
        db.get_conn(key)
        data = db.query(table="vshop_order",
                          columns=["id", "order_no", "state"],
                          order_by="-id",
                          limit=1)
        n -= 1
        db.put_conn(key)
        lst.append(key)
    print(time() - t)
    $: 8.982805013656616
    

    效果还是很明显的, 重复测试多次,倍数范围都在 15 倍左右

    顺便说下环境

    • 数据库是部署在 vmware 上面,所以查询时间跟本机有一定差距
    • 电脑 Miix 5, CPU i5-6500u
    • 性能均衡模式

    源码下载

    >>>原文地址<<<

    10 条回复    2017-04-11 09:20:44 +08:00
    welsmann
        1
    welsmann  
       2017-03-31 14:08:42 +08:00
    ....连接池不就是干这个的吗....
    dikT
        2
    dikT  
    OP
       2017-03-31 14:19:34 +08:00
    @welsmann 然而我们公司之前的框架都是实例化一个新对象...
    glasslion
        3
    glasslion  
       2017-03-31 15:54:15 +08:00   ❤️ 1
    Postgresql 连接池一般用 pgbounce 或 pgpool 之类的中间件
    dikT
        4
    dikT  
    OP
       2017-03-31 15:56:55 +08:00
    @glasslion tks,我去看看
    stabc
        5
    stabc  
       2017-03-31 16:17:08 +08:00
    我对这个概念不懂,这是不是一万次查询和一万次连接数据库的性能差别?
    imherer
        6
    imherer  
       2017-03-31 16:47:10 +08:00
    1069401249
        7
    1069401249  
       2017-03-31 18:37:32 +08:00   ❤️ 1
    没达到性能瓶颈吧,现在高并发的产品毕竟不多,所以你们还没做优化。。。
    dikT
        8
    dikT  
    OP
       2017-03-31 20:00:54 +08:00
    @1069401249 是啊,去哪儿找那么多高并发 ( ͡° ͜ʖ ͡°)
    AcmeSa
        9
    AcmeSa  
       2017-04-11 00:06:50 +08:00
    @welsmann S 的账号好象不能登录了,弱弱问一句,还能带我一起玩吗?
    dikT
        10
    dikT  
    OP
       2017-04-11 09:20:44 +08:00
    @AcmeSa 你在说啥..
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1103 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:13 · PVG 07:13 · LAX 16:13 · JFK 19:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.