python三种数据库连接池方式

psycopg2.pool – Connections pooling

Creating new PostgreSQL connections can be an expensive operation. This module offers a few pure Python classes implementing simple connection pooling directly in the client application.

class  psycopg2.pool. AbstractConnectionPool ( minconnmaxconn*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 ( connkey=Noneclose=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.

The following classes are AbstractConnectionPool subclasses ready to be used.

class  psycopg2.pool. SimpleConnectionPool ( minconnmaxconn*args**kwargs )

A connection pool that can’t be shared across different threads.

Note

 

This pool class is useful only for single-threaded applications.

class  psycopg2.pool. ThreadedConnectionPool ( minconnmaxconn*args**kwargs )

A connection pool that works with the threading module.

Note

 

This pool class can be safely used in multi-threaded applications.

class  psycopg2.pool. PersistentConnectionPool ( minconnmaxconn*args**kwargs )

A pool that assigns persistent connections to different threads.

Note that this connection pool generates by itself the required keys using the current thread id. This means that until a thread puts away a connection it will always get the same connection object by successive getconn() calls. This also means that a thread can’t use more than one single connection from the pool.

Note

 

This pool class is mostly designed to interact with Zope and probably not useful in generic applications.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值