设为首页 收藏本站
查看: 392|回复: 0

[经验分享] python dbpool

[复制链接]

尚未签到

发表于 2017-4-21 09:54:13 | 显示全部楼层 |阅读模式
  Trac back:http://jonpy.sourceforge.net/dbpool.html
dbpool module
Synopsis
  The dbpoolmodule is a wrapper for PythonDB-API 2.0-compliantdatabase modules to (a) keep a pool of physical connections available and (b)upgrade the modules to threadsafetylevel 2, whichmeans that threads can share logical database connections.
Usage
  The module provides only one function not described by theDB-API 2.0spec. The module is initialized by callingset_databasewith a reference to theunderlying database module to be used and the number of physical databaseconnections to keep in the pool. The dbpoolmodule is thenconfigured to act precisely like the underlying database module, except thatphysical database connections are not always closed when you have finished withthem, and if you try to create more than one cursor on a single logicalconnection, a new physical connection is created for the new cursor.
  When a logical database connection is no longer being used (i.e. you callthe closefunction or all references to the connection aredeleted), the physical connection is returned to the pool to be used the nexttime a connection is requested. If the pool is full (i.e. the limit set in thecall to set_databaseis reached) thenthe phsyical database connection is closed.
  Note that your code can either create one logical database connection, andthen create many cursors from that, or create many logical databaseconnections, and create one or more cursors from each of these. Either way thebehaviour of the dbpoolmodule will be the same - it will poolphsyical database connections, and only create one cursor per physicaldatabase connection.
  Example:
import jon.dbpool as dbpool
import MySQLdb, MySQLdb.cursors
dbpool.set_database(MySQLdb, 5)
dbh = dbpool.connect(user="example", passwd="s3cr3t", db="example",
  cursorclass=MySQLdb.cursors.DictCursor)
dbc1 = dbh.cursor()
dbc2 = dbh.cursor()
  Note that unfortunately, due to the way the DB-API works, once you haveconfigured dbpoolto act as a wrapper for a database module,you cannot re-configure with a different module.
Globals
Variables
apilevel
  The string 2.0, indicating the DB-API level.
threadsafety
  The integer 2, meaning that threads may shared the module,and database connections.
paramstyle
  This variable is only available afterset_databasehas been called. It willcontain the value from the underlying database module.
Warning
Error
InterfaceError
DatabaseError
DataError
OperationalError
IntegrityError
InternalError
ProgrammingError
NotSupportedError
  These exception classes are only available afterset_databasehas been called. Theyare copied from the underlying database module.
Functions
set_database(dbmod, minconns)
  dbmod: module
minconns: integer
  Configures the dbpoolmodule to act as a wrapper around thespecified DB-API module. dbmodis a reference to the databasemodule to use. minconnsis an integer, which must be 1 or greater,which indicates the number of physical database connections of each 'type' tokeep in the pool. Physical database connections are of the same 'type' if allthe parameters to the DB-API connectfunction are the same (i.e.they are connecting to the same database, on the same host, with the sameusername, with the same options).
  The database module to be used must have a threadsafetylevelof at least 1, i.e. if the database cannot cope at all withmulti-threading then there is nothing dbpoolcan do to make itwork.
  Note that you can only call this function once.
  Example:
set_database(MySQLdb, 5)
connect(...)
  Returns: database connection instance
  This function may only be used afterset_databasehas been called. Theparameters are dependent on the underlying database module being used.A logical database connection from the pool corresponding to databaseconnections with the parameters given is returned. If the pool is empty then anew physical connection is created.
  Example:
dbh = dbpool.connect(user="example", passwd="s3cr3t", db="example")
dbc = dbh.cursor()

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-367214-1-1.html 上篇帖子: python trick and track 下篇帖子: configparse python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表