pooling anything.
Project description
pooling
pooling anything.
Install
pip install pooling
Usage Example 1
import MySQLdb
from MySQLdb.cursors import DictCursor
from pooling import PoolBase
class MysqlConnectionPool(PoolBase):
def do_session_create(self, *create_args, **create_kwargs):
create_kwargs.setdefault("cursorclass", DictCursor)
create_kwargs.setdefault("autocommit", True)
create_kwargs.setdefault("charset", "utf8mb4")
return MySQLdb.connect(*create_args, **create_kwargs)
def do_session_destory(self, real_session):
real_session.close()
if __name__ == "__main__":
conn_info = {
"host": "127.0.0.1",
"port": 3306,
"user": "test",
"password": "test",
"db": "test",
}
pool = MysqlConnectionPool(pool_size=10, kwargs=conn_info)
connection = pool.get_session()
cursor = connection.cursor()
count = cursor.execute("show databases")
data = cursor.fetchall()
print("rows count=", count)
print("data=", data)
assert count == len(data)
Usage Example 2
import MySQLdb
from MySQLdb.cursors import DictCursor
from pooling import Pool
def mysql_conn_create():
conn_info = {
"host": "127.0.0.1",
"port": 3306,
"user": "test",
"password": "test",
"db": "test",
}
conn = MySQLdb.connect(cursorclass=DictCursor, autocommit=True, **conn_info)
return conn
def mysql_conn_close(session):
session.close()
if __name__ == "__main__":
pool = Pool(pool_size=10, create_factory=mysql_conn_create, destory_factory=mysql_conn_close)
connection = pool.get_session()
cursor = connection.cursor()
count = cursor.execute("show databases")
data = cursor.fetchall()
print("rows count=", count)
print("data=", data)
assert count == len(data)
Usage Example 3
conn_info = {
"host": "127.0.0.1",
"port": 3306,
"user": "test",
"password": "test",
"db": "test",
}
from pooling.mysql import MysqlConnectionPool
pool = MysqlConnectionPool(10, kwargs=conn_info)
connection = pool.get_session()
cursor = connection.cursor()
count = cursor.execute("show databases")
data = cursor.fetchall()
print("rows count=", count)
print("data=", data)
assert count == len(data)
Usage Example 4
conn_info = {
"host": "127.0.0.1",
"port": 3306,
"user": "test",
"password": "test",
"db": "test",
}
from pooling.mysql import MysqlConnectionPool
pool = MysqlConnectionPool(10, kwargs=conn_info)
data = pool.query("select * from table1")
for row in data:
print(row)
Note
- Call pool.get_session() returns a proxied connection instance.
- The returned proxied connection instance is a proxy instance of the real connection.
- When the returned proxied connection is being deleted, the real connection will be returned to the pool so that the real connection can be used again.
- A simple mysql connection pool can be imported by doing
from pooling.mysql import MysqlConnectionPool. Compare with theMysqlConnectionPoolimplemented inUsage Example 1, it add ping() test and errors handler in get_session() method. - The
pooling.mysqlmodule depends onmysqlclient, butpoolingis NOT, so thatmysqlclientis not auto installed afterpoolinginstalled. You should dopip install mysqlclientby yourself if you want to useMysqlConnectionPool. - If you want to use
MysqlConnectorPool, you need to install packagemysql-connector-pythonby yourself. - If you want to use
PyMySQLConnectionPool, you need to install packagePyMySQLby yourself.
Releases
v0.1.0
- First release.
v0.1.1
MysqlConnectionPool.get_session()doconnection.ping()to make sure the connection is avaiable.Pool.counterandPool.versionuse thread safe counter.
v0.1.2
- Fix problem in using
get_sessionwithwithstatement.with pool.get_session() as session: passgot the real session instread of our wrapped Session.
v0.1.5
- Fix problem in handling a proxied real-session instance.
- Fix un-sleep-problem while connecting to mysql server failed.
- Add python2.7 support.
v0.1.6
- Use zenutils.importutils.
- Fix get_session timeout calc problem.
v0.1.7
- Unit test passed.
v0.1.8
- Add _pooling_is_connection_closed test.
v0.1.9
- Fix doing useless time.sleep problem in pooling.mysql.MysqlConnectionPoolBase.get_session.
- Add pooling.mysql.PyMySQLConnectionPool.
v0.1.10
- Fix PoolBase.return_session double called in
with pool.get_session() as session: pass. - PoolBase add ping_test support.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pooling-0.1.10.tar.gz
(7.3 kB
view details)
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pooling-0.1.10.tar.gz.
File metadata
- Download URL: pooling-0.1.10.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48cb2dd50e31beaadfbd12b2bd5c3ee5ece95dae103ea2f4e5eddec6b88b7edb
|
|
| MD5 |
4d03831ee549ad54395edde2cc72dc24
|
|
| BLAKE2b-256 |
3b9741e1b06427c7c806f14d3732be1704b69f76234cdfbcd9b384fe6f9121cc
|
File details
Details for the file pooling-0.1.10-py3-none-any.whl.
File metadata
- Download URL: pooling-0.1.10-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c57ef747fc0114d8e8cd988b609fa7193c4ce48a6c9c07ae217ea54ed643f19
|
|
| MD5 |
e1bd6f68a31d321c3f5434934677ffe4
|
|
| BLAKE2b-256 |
efb03484c4a289873b7d6123e3f4de191060d545bc68d1ec241d0b181bf0517a
|
File details
Details for the file pooling-0.1.10-py2-none-any.whl.
File metadata
- Download URL: pooling-0.1.10-py2-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54cade3bab4661777bb2537a582a16983006f7307bb14ffdf75be9b8bc4fd30d
|
|
| MD5 |
922d2ffe2e37c590fb204c4310d8ea3e
|
|
| BLAKE2b-256 |
2c5a6af06e7fc8e32386264a7b9e23672c8134ac5d74855d139138b2bb7de2b0
|