pooling


Namepooling JSON
Version 0.1.11 PyPI version JSON
download
home_page
Summarypooling anything.
upload_time2023-09-14 12:46:01
maintainerSong Liang
docs_urlNone
authorSong Liang
requires_python
licenseMIT
keywords pooling
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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 the `MysqlConnectionPool` implemented in `Usage Example 1`, it add ping() test and errors handler in get_session() method.
* The `pooling.mysql` module depends on `mysqlclient`, but `pooling` is NOT, so that `mysqlclient` is not auto installed after `pooling` installed. You should do `pip install mysqlclient` by yourself if you want to use `MysqlConnectionPool`.
* If you want to use `MysqlConnectorPool`, you need to install package `mysql-connector-python` by yourself.
* If you want to use `PyMySQLConnectionPool`, you need to install package `PyMySQL` by yourself.

## Releases

### v0.1.0

- First release.

### v0.1.1

- `MysqlConnectionPool.get_session()` do `connection.ping()` to make sure the connection is avaiable.
- `Pool.counter` and `Pool.version` use thread safe counter.

### v0.1.2

- Fix problem in using `get_session` with `with` statement. `with pool.get_session() as session: pass` got 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.

### v0.1.11

- Doc update.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pooling",
    "maintainer": "Song Liang",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "songliang@zencore.cn",
    "keywords": "pooling",
    "author": "Song Liang",
    "author_email": "songliang@zencore.cn",
    "download_url": "https://files.pythonhosted.org/packages/81/60/9c909498108203f1839681a2523ef704633a32828ae9f0de761d7c57e544/pooling-0.1.11.tar.gz",
    "platform": null,
    "description": "# pooling\n\npooling anything.\n\n## Install\n\n```\npip install pooling\n```\n\n## Usage Example 1\n\n\n```\nimport MySQLdb\nfrom MySQLdb.cursors import DictCursor\n\nfrom pooling import PoolBase\n\nclass MysqlConnectionPool(PoolBase):\n    \n    def do_session_create(self, *create_args, **create_kwargs):\n        create_kwargs.setdefault(\"cursorclass\", DictCursor)\n        create_kwargs.setdefault(\"autocommit\", True)\n        create_kwargs.setdefault(\"charset\", \"utf8mb4\")\n        return MySQLdb.connect(*create_args, **create_kwargs)\n\n    def do_session_destory(self, real_session):\n        real_session.close()\n\nif __name__ == \"__main__\":\n    conn_info = {\n        \"host\": \"127.0.0.1\",\n        \"port\": 3306,\n        \"user\": \"test\",\n        \"password\": \"test\",\n        \"db\": \"test\",\n    }\n    pool = MysqlConnectionPool(pool_size=10, kwargs=conn_info)\n    connection = pool.get_session()\n    cursor = connection.cursor()\n    count = cursor.execute(\"show databases\")\n    data = cursor.fetchall()\n    print(\"rows count=\", count)\n    print(\"data=\", data)\n    assert count == len(data)\n\n```\n\n## Usage Example 2\n\n```\nimport MySQLdb\nfrom MySQLdb.cursors import DictCursor\n\nfrom pooling import Pool\n\ndef mysql_conn_create():\n    conn_info = {\n        \"host\": \"127.0.0.1\",\n        \"port\": 3306,\n        \"user\": \"test\",\n        \"password\": \"test\",\n        \"db\": \"test\",\n    }\n    conn = MySQLdb.connect(cursorclass=DictCursor, autocommit=True, **conn_info)\n    return conn\n\ndef mysql_conn_close(session):\n    session.close()\n\nif __name__ == \"__main__\":\n    pool = Pool(pool_size=10, create_factory=mysql_conn_create, destory_factory=mysql_conn_close)\n    connection = pool.get_session()\n    cursor = connection.cursor()\n    count = cursor.execute(\"show databases\")\n    data = cursor.fetchall()\n    print(\"rows count=\", count)\n    print(\"data=\", data)\n    assert count == len(data)\n```\n\n## Usage Example 3\n\n```\nconn_info = {\n    \"host\": \"127.0.0.1\",\n    \"port\": 3306,\n    \"user\": \"test\",\n    \"password\": \"test\",\n    \"db\": \"test\",\n}\n\nfrom pooling.mysql import MysqlConnectionPool\n\npool = MysqlConnectionPool(10, kwargs=conn_info)\nconnection = pool.get_session()\ncursor = connection.cursor()\ncount = cursor.execute(\"show databases\")\ndata = cursor.fetchall()\nprint(\"rows count=\", count)\nprint(\"data=\", data)\nassert count == len(data)\n```\n\n## Usage Example 4\n\n```\nconn_info = {\n    \"host\": \"127.0.0.1\",\n    \"port\": 3306,\n    \"user\": \"test\",\n    \"password\": \"test\",\n    \"db\": \"test\",\n}\n\nfrom pooling.mysql import MysqlConnectionPool\n\npool = MysqlConnectionPool(10, kwargs=conn_info)\ndata = pool.query(\"select * from table1\")\nfor row in data:\n    print(row)\n\n```\n\n## Note\n\n* Call pool.get_session() returns a proxied connection instance.\n* The returned proxied connection instance is a proxy instance of the real connection.\n* 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.\n* A simple mysql connection pool can be imported by doing `from pooling.mysql import MysqlConnectionPool`. Compare with the `MysqlConnectionPool` implemented in `Usage Example 1`, it add ping() test and errors handler in get_session() method.\n* The `pooling.mysql` module depends on `mysqlclient`, but `pooling` is NOT, so that `mysqlclient` is not auto installed after `pooling` installed. You should do `pip install mysqlclient` by yourself if you want to use `MysqlConnectionPool`.\n* If you want to use `MysqlConnectorPool`, you need to install package `mysql-connector-python` by yourself.\n* If you want to use `PyMySQLConnectionPool`, you need to install package `PyMySQL` by yourself.\n\n## Releases\n\n### v0.1.0\n\n- First release.\n\n### v0.1.1\n\n- `MysqlConnectionPool.get_session()` do `connection.ping()` to make sure the connection is avaiable.\n- `Pool.counter` and `Pool.version` use thread safe counter.\n\n### v0.1.2\n\n- Fix problem in using `get_session` with `with` statement. `with pool.get_session() as session: pass` got the real session instread of our wrapped Session.\n\n### v0.1.5\n\n- Fix problem in handling a proxied real-session instance.\n- Fix un-sleep-problem while connecting to mysql server failed.\n- Add python2.7 support.\n\n### v0.1.6\n\n- Use zenutils.importutils.\n- Fix get_session timeout calc problem.\n\n### v0.1.7\n\n- Unit test passed.\n\n### v0.1.8\n\n- Add _pooling_is_connection_closed test.\n\n### v0.1.9\n\n- Fix doing useless time.sleep problem in pooling.mysql.MysqlConnectionPoolBase.get_session.\n- Add pooling.mysql.PyMySQLConnectionPool.\n\n### v0.1.10\n\n- Fix PoolBase.return_session double called in `with pool.get_session() as session: pass`.\n- PoolBase add ping_test support.\n\n### v0.1.11\n\n- Doc update.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "pooling anything.",
    "version": "0.1.11",
    "project_urls": null,
    "split_keywords": [
        "pooling"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "554ad4726abd0332da333ec9d772100dc5437b8eb4bee333d965d0b955762d55",
                "md5": "5acad30be463dc784906cdb9bc7f0a30",
                "sha256": "3e3a762832deb3d8a98ec07c5b027462369f5abd6b6183a030d1e5f0fea9b298"
            },
            "downloads": -1,
            "filename": "pooling-0.1.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5acad30be463dc784906cdb9bc7f0a30",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7500,
            "upload_time": "2023-09-14T12:45:59",
            "upload_time_iso_8601": "2023-09-14T12:45:59.696714Z",
            "url": "https://files.pythonhosted.org/packages/55/4a/d4726abd0332da333ec9d772100dc5437b8eb4bee333d965d0b955762d55/pooling-0.1.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "81609c909498108203f1839681a2523ef704633a32828ae9f0de761d7c57e544",
                "md5": "32501586e6cb914ab48c2ba001feff27",
                "sha256": "100e7569a75c509023b8bd5eb65d130ffc156211827539797f3e80de0ba5f02e"
            },
            "downloads": -1,
            "filename": "pooling-0.1.11.tar.gz",
            "has_sig": false,
            "md5_digest": "32501586e6cb914ab48c2ba001feff27",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7370,
            "upload_time": "2023-09-14T12:46:01",
            "upload_time_iso_8601": "2023-09-14T12:46:01.717171Z",
            "url": "https://files.pythonhosted.org/packages/81/60/9c909498108203f1839681a2523ef704633a32828ae9f0de761d7c57e544/pooling-0.1.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-14 12:46:01",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pooling"
}
        
Elapsed time: 0.11702s