python psycopg2 连接pg 建立连接池

摘要:
#-*-编码:utf-8-*-frompsycopg2.poolimportThreadConnectionPool、SimpleConnectionPool和PersistentConnectionPoolfromconstanimportpg_name、pg_user、pg_pw、pg_host、pg_portfrompublicimportgen_sql#pgpool=Three

# -*- coding: utf-8 -*-
from psycopg2.pool import ThreadedConnectionPool,SimpleConnectionPool,PersistentConnectionPool

from constant import pg_name, pg_user, pg_pw, pg_host, pg_port
from public import gen_sql

# pgpool = ThreadedConnectionPool(1, 5, dbname=pg_name, user=pg_user, host=pg_host, password=pg_pw, port=pg_port)

# pgpool = SimpleConnectionPool(1, 5, dbname=pg_name, user=pg_user, host=pg_host, password=pg_pw, port=pg_port)
pgpool = PersistentConnectionPool(1, 100, dbname=pg_name, user=pg_user, host=pg_host, password=pg_pw, port=pg_port)

# 不管是哪种方式建立的连接池, 多进程或者多线程都会导致数据cursor 关闭, 出错等数据库问题, 即使是在每个进程中都建立连接池也不行(我的测试结果, 水平有限)

def conn_exe(*sp):
  conn = pgpool.getconn()  # 获取连接
  cursor = conn.cursor()  # 获取cursor
  cursor.execute(*sp)
  conn.commit()  # 没次操作都要提交
  pgpool.putconn(conn)  # 放回连接, 防止其他程序pg无连接可用
  return cursor


def fetchone_sql(*sp):
  cursor = conn_exe(*sp)
  # desc = cursor.description  # cursor 的具体描述信息
  fetchone = cursor.fetchone()
  cursor.close()
  return fetchone


def fetchall_sql(*sp):

  cursor = conn_exe(*sp)
  fetchall = cursor.fetchall()
  cursor.close()
  return fetchall


def get_insert_id(*sp): 

  *sp += " returning id"   # 插入语句这样返回 插入的id(或者其他字段 看上一行的SQL 语句) 

  cursor = conn_exe(*sp)
  insert_id = cursor.fetchone()[0]
  cursor.close()
  return insert_id


def run_sql(*sp): 

  cursor = conn_exe(*sp)
  cursor.close()

免责声明:文章转载自《python psycopg2 连接pg 建立连接池》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇在C#中快速查询文件ubuntu18.04server服务器系统下为python安装虚拟显示器 (使用jupyter notebook在web端播放openai的gym下保存的运行视频——需安装ipython)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决 No module named 'airtest.core'

当前环境mac os 1、已pip install airtest 2、可以通过命令行运行 airtest的demo,但是使用pycharm 运行会提示No module named 'airtest.core' 3、当前pycharm 解释器已设置为 自己安装的python版本 4、在sitepackage 中可见 airtest的包,在pychram中...

Docker Compose

Docker Compose简介 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。从功能上看,跟 OpenStack 中的 Heat 十分类似。 其代码目前在 https://github.com/docker/compose 上开源。 Compose 定位是 「定义和运行多个 Docker 容器的应用(...

difflib模块文件内容差异对比

简介 difflib作为python的标准库模块,无需安装,作用是比对文本之间的差异,且支持输出可读性比较强的HTML文档,与Linux下的diff命令相似。可以使用该模块比对代码和配置文件的差异,在版本控制方面非常有用。Python2.3以后的版本默认自带difflib模块,无需额外安装。 使用方法 字符串差异的比对 #!/usr/bin/env py...

MonkeyRunner (一)

monkeyrunner The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. With monkeyrunner, you can write a...

(python learn) 5 元组

首先我们要知道,字符串,元组,还有列表等数据类型在python中都属于序列数据类型。对这种数据类型,有一些统一的函数可用,比如: len() 可以返回长度 +可以连接两个序列 *可以重复两个序列中的元素 in可以判断某个元素是否在序列中 max()返回最大元素 min()返回最小元素 cmp()比较两个序列是否相等 下面,我们研究一下元组。 元组是一组被逗...

几种常用库在CentOS下的编译

1操作环境 通过命令查看操作系统版本信息: [root@localhost ~]# cat /proc/version Linux version 3.10.0-327.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (G...