创建新的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

psycopg2.pool – Connections pooling / psycopg2.pool – 连接池 / postgresql 连接池的更多相关文章

  1. 帆软报表FineReport中数据连接的JDBC连接池属性问题

    连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...

  2. 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 数据库连接不释放测试 连接池 释放连接 关闭连接 有关 redis-py 连接池会导致服务器产生大量 CLOSE_WAIT 的再讨论以及一个解决方案

    import pymysqlfrom redis import Redisimport time h, pt, u, p, db = '192.168.2.210', 3306, 'root', 'n ...

  3. commons-pool与commons-pool2连接池(Hadoop连接池)

    commons-pool和commons-pool2是用来建立对象池的框架,提供了一些将对象池化必须要实现的接口和一些默认动作.对象池化之后可以通过pool的概念去管理其生命周期,例如对象的创建,使用 ...

  4. JDBC连接池-自定义连接池

    JDBC连接池 java JDBC连接中用到Connection   在每次对数据进行增删查改 都要 开启  .关闭  ,在实例开发项目中 ,浪费了很大的资源 ,以下是之前连接JDBC的案例 pack ...

  5. Http持久连接与HttpClient连接池

    一.背景 HTTP协议是无状态的协议,即每一次请求都是互相独立的.因此它的最初实现是,每一个http请求都会打开一个tcp socket连接,当交互完毕后会关闭这个连接. HTTP协议是全双工的协议, ...

  6. Http 持久连接与 HttpClient 连接池

    一.背景 HTTP协议是无状态的协议,即每一次请求都是互相独立的.因此它的最初实现是,每一个http请求都会打开一个tcp socket连接,当交互完毕后会关闭这个连接. HTTP协议是全双工的协议, ...

  7. Mysql线程池系列一:什么是线程池和连接池( thread_pool 和 connection_pool)

       thread_pool 和 connection_pool 当客户端请求的数据量比较大的时候,使用线程池可以节约大量的系统资源,使得更多的CPU时间和内存可以高效地利用起来.而数据库连接池的使用 ...

  8. (转)WebSphere 中池资源调优 - 线程池、连接池和 ORB

    WebSphere 中池资源调优 - 线程池.连接池和 ORB 来自:https://www.ibm.com/developerworks/cn/websphere/library/techartic ...

  9. django更换ORM连接处理(连接池)转

    1 概述 在使用 Django 进行 Web 开发时, 我们避免不了与数据库打交道. 当并发量低的时候, 不会有任何问题. 但一旦并发量达到一定数量, 就会导致 数据库的连接数会被瞬时占满. 这将导致 ...

随机推荐

  1. [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine)

    [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine) http://blogs.technet. ...

  2. 1564: [NOI2009]二叉查找树 - BZOJ

    Description Input Output只有一个数字,即你所能得到的整棵树的访问代价与额外修改代价之和的最小值.Sample Input4 101 2 3 41 2 3 41 2 3 4Sam ...

  3. bzoj 1024 暴力深搜

    我们直接暴力的深搜怎么切就行了, 每一刀切的方案只有横着和竖着,横竖又分在几等分点切, 因为要保证每个人的面积相同,所以比较好处理了,第几个几等分点就 分给这边几刀. /*************** ...

  4. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  5. idea新建项目完整过程

    参看下面博客 http://www.cnblogs.com/cnjava/archive/2013/01/29/2881654.html 突然,感觉idea其实挺麻烦的: 一.junit test做起 ...

  6. Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...

  7. javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现

    线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...

  8. GA项目体会

    1.NaN表示运算的结果是未定义的计算过程,例如0/0.在计算EBO的时候,由于使用泊松分布的计算过程,出现了0/0的情况,所以控制台才会提示"非数字". 2.保障资金太小的时候可 ...

  9. 1234['toString']['length'] 等于啥?

    首先说该题的答案:1 这是我在一问一答上做的一个选择题,题目虽小,但是包含内容很多,很多网友斥责此题操蛋,恶心.其实只要我们细心的去理解,这段代码有很多值得我们记住的知识点. 1.[]的作用 []是j ...

  10. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...