pax_global_header00006660000000000000000000000064141336565740014530gustar00rootroot0000000000000052 comment=12fa2306d59dba612420134818d7e21cee60731e python-connection-pool-0.0.3/000077500000000000000000000000001413365657400161555ustar00rootroot00000000000000python-connection-pool-0.0.3/.gitignore000066400000000000000000000022731413365657400201510ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .vscode python-connection-pool-0.0.3/LICENSE000066400000000000000000000020471413365657400171650ustar00rootroot00000000000000MIT License Copyright (c) 2018 ZhouYL Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. python-connection-pool-0.0.3/README.rst000066400000000000000000000020101413365657400176350ustar00rootroot00000000000000ConnectionPool ############## Thread-safe connection pool for python Install ======= .. code-block:: bash $ pip install connection_pool Examples ======== Create a pool --------------- .. code-block:: python import memcache from connection_pool import ConnectionPool # via create function def create_memcache_client(): return memcache.Client(['127.0.0.1:11211']) pool = ConnectionPool(create=create_memcache_client, max_size=10, max_usage=10000, idle=60, ttl=120) # via lambda pool = ConnectionPool(create=lambda: memcache.Client(['127.0.0.1:11211']), max_size=10) # via functools.partial from functools import partial pool = ConnectionPool(create=partial(memcache.Client, ['127.0.0.1:11211']), max_size=10) # using a connection with pool.item() as memcache: memcache.set('foo', 'bar') License ======= The MIT License (MIT). Please see License File for more information. python-connection-pool-0.0.3/connection_pool/000077500000000000000000000000001413365657400213455ustar00rootroot00000000000000python-connection-pool-0.0.3/connection_pool/__init__.py000066400000000000000000000151511413365657400234610ustar00rootroot00000000000000# -*- coding: utf-8 -*- import threading import time import queue class TooManyConnections(Exception): '''当连接数过多时,抛出该异常''' class Expired(Exception): '''当连接不可用时,抛出该异常''' class UsageExceeded(Expired): '''连接的使用次数超出限制''' class TtlExceeded(Expired): '''连接使用时间超出 ttl 指定的生命周期''' class IdleExceeded(Expired): '''闲置时间超出 idle 指定的时间''' class WrapperConnection(object): '''用于包装连接池中的数据库连接,以便处理生命周期逻辑''' def __init__(self, pool, connection): self.pool = pool self.connection = connection self.usage = 0 self.last = self.created = time.time() def using(self): '''当连接被调用时使用该方法,使用次数 +1''' self.usage += 1 self.last = time.time() return self def reset(self): '''重置连接包装状态''' self.usage = self.last = self.created = 0 def __enter__(self): return self.connection def __exit__(self, *exc_info): self.pool.release(self) class ConnectionPool(object): '''连接池类,可用于 pymysql/memcache/redis/... 等 可通过如下方式调用: pool = ConnectionPool(create=redis.Redis) 也可以通过 lambda 的方式指定 create 调用: pool = ConnectionPool(create=lambda: redis.Redis(host='127.0.0.1')) 或者通过 functools.partial from functools import partial pool = ConnectionPool(create=partial(redis.Redis, host='127.0.0.1')) ''' __wrappers = {} def __init__(self, create, close=None, max_size=10, max_usage=0, ttl=0, idle=60, block=True): '''初始化参数 create: 必须是一个可 callback 的函数 must be a callback function, creates connection close: optional callback to close connection max_size: 最大连接数,当为 0 的时候则没有限制,不建议设置为 0 the maximum number of connections, 0 is no limit (not recommended) max_usage: 连接可使用次数,达到该次数后,连接将被释放/关闭 the number of times the connection can be used, after this number of times, the connection will be released/closed ttl: 连接可使用时间,单位(秒),当连接使用达到指定时间后, 连接将被释放/关闭 connection use time limit (seconds), when reached, the connection will be released/closed idle: 连接空闲时间,单位(秒),当连接在闲置指定时间后,将被释放/关闭 connection idle time (seconds), when reached, the connection will be released/closed block: 当连接数满的时候,是否阻塞等待连接被释放, 输入 False 则在连接池满时会抛出异常 When the number of connections is full, whether to block and wait for the connection to be released. Enter False to throw an exception when the connection pool is full ''' if not hasattr(create, '__call__'): raise ValueError('"create" argument is not callable') if close is not None and not hasattr(close, '__call__'): raise ValueError('"close" argument is not callable') self._create = create self._close = close self._max_size = int(max_size) self._max_usage = int(max_usage) self._ttl = int(ttl) self._idle = int(idle) self._block = bool(block) self._lock = threading.Condition() self._pool = queue.Queue() self._size = 0 def item(self): '''可通过 with ... as ... 语法调用 pool = ConnectionPool(create=redis.Redis) with pool.item() as redis: redis.set('foo', 'bar) ''' self._lock.acquire() try: while (self._max_size and self._pool.empty() and self._size >= self._max_size): if not self._block: raise TooManyConnections('Too many connections') self._lock.wait() # 等待闲置连接 try: wrapped = self._pool.get_nowait() # 从空闲连接池中获取一个 if self._idle and (wrapped.last + self._idle) < time.time(): self._destroy(wrapped) raise IdleExceeded('Idle exceeds %d secs' % self._idle) except (queue.Empty, IdleExceeded): wrapped = self._wrapper(self._create()) # 创建新连接 self._size += 1 finally: self._lock.release() return wrapped.using() def release(self, conn): '''释放一个连接,让连接重回到连接池中 当连接使用超过限制/超过限定时间时,连接将被销毁 ''' self._lock.acquire() wrapped = self._wrapper(conn) try: self._test(wrapped) except Expired: self._destroy(wrapped) else: self._pool.put_nowait(wrapped) self._lock.notifyAll() # 通知其它线程,有闲置连接可用 finally: self._lock.release() def _destroy(self, wrapped): '''销毁一个连接''' if self._close: self._close(wrapped.connection) self._unwrapper(wrapped) self._size -= 1 def _wrapper(self, conn): '''利用 id 地址,对连接进行包装''' if isinstance(conn, WrapperConnection): return conn _id = id(conn) if _id not in self.__wrappers: self.__wrappers[_id] = WrapperConnection(self, conn) return self.__wrappers[_id] def _unwrapper(self, wrapped): '''取消对连接的包装''' if not isinstance(wrapped, WrapperConnection): return _id = id(wrapped.connection) wrapped.reset() del wrapped if _id in self.__wrappers: del self.__wrappers[_id] def _test(self, wrapped): '''测试连接的可用性,当不可用时,抛出 Expired 异常''' if self._max_usage and wrapped.usage >= self._max_usage: raise UsageExceeded('Usage exceeds %d times' % self._max_usage) if self._ttl and (wrapped.created + self._ttl) < time.time(): raise TtlExceeded('TTL exceeds %d secs' % self._ttl) python-connection-pool-0.0.3/setup.py000066400000000000000000000017751413365657400177010ustar00rootroot00000000000000#!/usr/bin/env python # coding=utf-8 from setuptools import setup, find_packages setup( name='connection_pool', version='0.0.3', description='thread safe connection pool', long_description=open('README.rst').read(), author='zhouyl', author_email='81438567@qq.com', license='MIT', packages=find_packages(), url='https://github.com/zhouyl/ConnectionPool', classifiers=[ 'Operating System :: OS Independent', 'Intended Audience :: Developers', "License :: OSI Approved :: MIT License", 'Programming Language :: Python', 'Programming Language :: Python :: Implementation', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Topic :: Software Development :: Libraries :: Python Modules' ], )