创建新的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. HDU 5781 ATM Mechine 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 ATM Mechine Time Limit: 6000/3000 MS (Java/Othe ...

  2. hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:比较简单,易懂,这里不做说明. 解法:第一种方法:用map映射,耗时1000+ms:第二种 ...

  3. 剑指offer--3题

    题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, ...

  4. 2016ACM-ICPC Qingdao Online青岛网络赛题解

    TonyFang+Sps+我=5/12 滚了个大粗 2016年9月21日16:42:36 10题完工辣 01 题意:求形同的数中大于n的最小值 题解:预处理所有的(5194个),在这里面二分 #inc ...

  5. Spring声明式事务配置管理方法(转)

    项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...

  6. UML部署图(转载)

    概述: 部署图用于可视化的软件组件部署的系统中的物理组件的拓扑结构. 因此,部署图是用来描述一个系统的静态部署视图.部署图由节点和它们之间的关系. 目的: 部署名称本身描述的原理图的目的.部署图用于描 ...

  7. SWF Web播放器

    <HTML> <HEAD> <!-- saved from url=(0013)about:internet --> <TITLE> Untitled. ...

  8. (转)約瑟夫問題的兩個O(log n)解法

    約瑟夫問題的兩個O(log n)解法 這個是學習編程時的一個耳熟能詳的問題了: n個人(編號爲0,1,...,n-1)圍成一個圈子,從0號開始依次報數,每數到第m個人,這個人就得自殺, 之後從下個人開 ...

  9. POJ 2195

    #include<iostream>//by Chengdacaizi #include<stdio.h> #include<vector> #include< ...

  10. java基础知识回顾之javaIO类---InputStreamReader和OutputStreamWriter转化流

    InputStreamReader:是字节流通向字符流的桥梁: OutputStreamWriter 是字符流通向字节流的桥梁: package com.lp.ecjtu; import java.i ...