zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
U
M±d<ã@s<dZddlmZddlmZGdd„dƒZGdd„dƒZd    S)
añ    SimplePooledPg - a very simple classic PyGreSQL connection pool.
 
Implements a pool of threadsafe cached connections
to a PostgreSQL database which are transparently reused,
using the classic (not DB-API 2 compliant) PyGreSQL pg API.
 
This should result in a speedup for persistent applications
such as the "Webware for Python" AppServer.
 
For more information on PostgreSQL, see:
    https://www.postgresql.org/
For more information on PyGreSQL, see:
    http://www.pygresql.org
For more information on Webware for Python, see:
    https://webwareforpython.github.io/w4py/
 
Measures are taken to make the pool of connections threadsafe
regardless of the fact that the PyGreSQL pg module itself is
not threadsafe at the connection level.  Connections will never be
shared between threads, so you can safely use transactions.
 
Usage:
 
The idea behind SimplePooledPg is that it's completely transparent.
After you have established your connection pool, stating the
number of connections to be cached in the pool and the
connection parameters, e.g.
 
    from dbutils.simple_pooled_pg import PooledPg
    dbpool = PooledPg(5, host=..., database=..., user=..., ...)
 
you can demand database connections from that pool,
 
    db = dbpool.connection()
 
and use them just as if they were ordinary PyGreSQL pg API
connections.  It's really just a proxy class.
 
db.close() will return the connection to the pool, it will not
actually close it.  This is so your existing code works nicely.
 
Ideas for improvement:
 
* Do not create the maximum number of connections on startup
already, but only a certain number and the rest on demand.
* Detect and transparently reset "bad" connections.  The PyGreSQL
pg API provides a status attribute and a reset() method for that.
* Connections should have some sort of "maximum usage limit"
after which they should be automatically closed and reopened.
* Prefer or enforce thread affinity for the connections.
 
Please note that these and other ideas have been already
implemented in in PooledPg, a more sophisticated version
of SimplePooledPg.  You might also consider using PersistentPg
instead for thread-affine persistent PyGreSQL connections.
SimplePooledPg may still serve as a very simple reference
and example implementation for developers.
 
 
Copyright, credits and license:
 
* Contributed as supplement for Webware for Python and PyGreSQL
  by Christoph Zwerschke in September 2005
* Based on the code of DBPool, contributed to Webware for Python
  by Dan Green in December 2000
 
Licensed under the MIT license.
é)ÚDBé)Ú __version__c@s0eZdZdZdd„Zdd„Zdd„Zdd    „Zd
S) ÚPooledPgConnectionz™A proxy class for pooled PostgreSQL connections.
 
    You don't normally deal with this class directly,
    but use PooledPg to get new connections.
    cCs||_||_dS©N)Ú_conÚ_pool)ÚselfÚpoolÚcon©r úOd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\dbutils/simple_pooled_pg.pyÚ__init__RszPooledPgConnection.__init__cCs"|jdk    r|j |j¡d|_dS)zClose the pooled connection.N)rrÚcache©r    r r r ÚcloseVs
zPooledPgConnection.closecCs t|j|ƒSr)Úgetattrr)r    Únamer r r Ú __getattr__^szPooledPgConnection.__getattr__cCs | ¡dSr)rrr r r Ú__del__bszPooledPgConnection.__del__N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrrrr r r r rKs
rc@s,eZdZdZeZdd„Zdd„Zdd„ZdS)    ÚPooledPgz’A very simple PostgreSQL connection pool.
 
    After you have created the connection pool,
    you can get connections using getConnection().
    cOs^zddlm}Wn tk
r0ddlm}YnX||ƒ|_t|ƒD]}| t||Ž¡qDdS)zôSet up the PostgreSQL connection pool.
 
        maxconnections: the number of connections cached in the pool
        args, kwargs: the parameters that shall be used to establish
            the PostgreSQL connections using pg.connect()
        r)ÚQueueN)rÚ ImportErrorÚqueueÚ_queueÚrangerÚ PgConnection)r    ZmaxconnectionsÚargsÚkwargsrÚir r r ros
 
 zPooledPg.__init__cCs|j |¡dS)z'Add or return a connection to the pool.N)rÚput)r    r r r r rƒszPooledPg.cachecCst||j ¡ƒS)zGet a connection from the pool.)rrÚgetrr r r Ú
connection‡szPooledPg.connectionN)    rrrrrÚversionrrr&r r r r rfs
rN)rZpgrr Úrrrr r r r Ú<module>sE