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)
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.
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.
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.1.tar.gz
(5.0 kB
view details)
Built Distribution
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.1.tar.gz.
File metadata
- Download URL: pooling-0.1.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3daada0a122e3e47538649f4d871752e0200782e3e77b151d94649fa54f82592
|
|
| MD5 |
19c4783545feba2782cef677a30e602a
|
|
| BLAKE2b-256 |
af30781fed76ab35257c10196b6c0ca20749233e0f628469a81503f495d10336
|
File details
Details for the file pooling-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pooling-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2192dd755ad95583efee8063b12c8df26354771b0176d44a4299c73baf2aaf37
|
|
| MD5 |
da18cb6541b2b0a7778573e3465ce21b
|
|
| BLAKE2b-256 |
223ca041d663918f825a1191a33c95d5c995a9d51dd7c2cf261280c523c4180b
|