psycopg2.pool – Connections pooling / psycopg2.pool – 连接池 / postgresql 连接池

创建新的PostgreSQL连接可以是一个昂贵的操作。这个模块提供了一些纯Python类直接在客户端应用程序实现简单的连接池。

    

class psycopg2.pool.AbstractConnectionPool(minconn, maxconn, *args, **kwargs)

      基类实现通用的基于密钥池代码。

      自动创建新的minconn连接。池将支持的最大maxconn连接。* args,* * kwargs传递到connect()函数。

      以下预计将由子类实现方法:

  

getconn(key=None)

   得到一个空连接,并将其分配给key如果不是没有。

putconn(conn, key=None, close=False)

     put away 一个连接。

  如果关闭是真的,则放弃池中的连接。

 

 closeall()

         关闭池处理的所有连接。

         请注意所有的连接都关闭,包括最终在应用程序中使用的连接。
        下面的类,子类可以使用abstractconnectionpool。

class psycopg2.pool.SimpleConnectionPool(minconn, maxconn, *args, **kwargs)

      不能在不同线程中共享的连接池。

      请注意,这个池类仅用于单线程应用程序。

class psycopg2.pool.ThreadedConnectionPool(minconn, maxconn, *args, **kwargs)

      一个连接池与线程模块一起工作。

      注意这个 池  类 可以安全地应用在多线程应用程序中。

class psycopg2.pool.PersistentConnectionPool(minconn, maxconn, *args, **kwargs)

 

分配给不同线程的持久连接的池。
注意,这个连接池产生本身所需要的密钥使用当前线程ID。这意味着,直到一个线程把连接将永远得到一个相同的连接对象的连续getconn()  calls。这也意味着一个线程不能使用从池中的一个以上的单个连接。

注意: 这个连接池类主要被设计去和Zope连接 ,在一些通用的应用程序中很可能不是实用的

 

下面是源代码

 

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(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 **kwargs are 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.

The following classes are AbstractConnectionPool subclasses ready to be used.

class psycopg2.pool.SimpleConnectionPool(minconn, maxconn, *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(minconn, maxconn, *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(minconn, maxconn, *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.
Previous topic

 

转载于:https://www.cnblogs.com/xuchunlin/p/5631096.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值