{ "info": { "author": "ljk", "author_email": "chaoyuemyself@hotmail.com", "bugtrack_url": null, "classifiers": [], "description": "# PyMySQL_Connection_Pool\nA simple connection pool based PyMySQL. Mainly focus on **multi threads** mode when use `pymysql`, but also compatible with single thread mode for convenience when you need to use these two mode together. Within multi threads mode support the multiplexing similar feature(when use connection with `Context Manager Protocol`).\n\nProblem: When use pymysql with python multi threads, generally we will face the questions:\n1. It can't share a connection created by main thread with all sub-threads. It will result error like this:\n`pymysql.err.InternalError: Packet sequence number wrong - got 0 expected 1`\n2. If we make every sub-thread to create a connection and close it when this sub-thread end, that's workable but obviously lead to high cost on establish connections with MySQL.\n\nSo I implement this module aimed at create as least connections as possible with MySQL in multi-threads programing. \n\nThis module contain two class: \n- `Connection` is a subclass of `pymysql.connections.Connection`, it can use with or without connection_pool, **It's usage is all the same with pymysql**. The detail(when with connection_pool, it should take additional action to maintain the pool) implement about connection pool is hiddened. \nThis class provide a wrapped execute_query() method for convenience, which take several parameters.\n- `ConnectionPool`'s instance represent the real connection_pool.\n\n## Use example\n### Install\n```\npip install pymysql-pool\n```\n### multi-threads mode: \nThe mainly difference with single-thread mode is that we should maintain the status of the pool. Such as 'get connection from pool' or 'put connection back to pool', in which case there are also some case to deal, such as: \n- when get connection from a pool: we should deal with the **timeout** and **retry** parameters\n- when put connection back to pool: if we executed queries without exceptions, this connection can go back to pool directly; but if **exception** occurred, we should decided whether this connection should go back to pool depend on if it is **reusable**(base on the exception type). If the connection shouldn't bo back to pool, we close it and **recreate** a new connection then put it to the pool.\n\nLuckily, this module will take care of these complicated details for you automatic.\n\nThere also can create more than one connection_pool(with distinct `ConnectionPool.name` attribute) to associate with different databases.\n\nIn the example below, we will see how it work within connection_pool feature: \n```\n>>> import pymysqlpool\n>>> pymysqlpool.logger.setLevel('DEBUG')\n>>> config={'host':'xxxx', 'user':'xxx', 'password':'xxx', 'database':'xxx', 'autocommit':True}\n\n### Create a connection pool with 2 connection in it\n>>> pool1 = pymysqlpool.ConnectionPool(size=2, name='pool1', **config)\n>>> pool1.size()\n2\n>>> con1 = pool1.get_connection()\n2017-12-25 21:38:48 DEBUG: Get connection from pool(pool1)\n>>> con2 = pool1.get_connection()\n2017-12-25 21:38:51 DEBUG: Get connection from pool(pool1)\n>>> pool1.size()\n0\n\n### We can prophesy that here will occur some exception, because the pool1 is empty\n>>> con3 = pool1.get_connection(timeout=0, retry_num=0)\nTraceback (most recent call last):\n File \"e:\\github\\pymysql-pool\\pymysqlpool.py\", line 115, in get_connection\n conn = self._pool.get(timeout=timeout) if timeout > 0 else self._pool.get_nowait()\nqueue.Empty\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"\", line 1, in \n con3 = pool1.get_connection(timeout=0, retry_num=0)\n File \"e:\\github\\pymysql-pool\\pymysqlpool.py\", line 128, in get_connection\n self.name, timeout, total_times))\npymysqlpool.GetConnectionFromPoolError: can't get connection from pool(pool1) within 0*1 second(s)\n\n### Now let's see the connection's behavior when call close() method and use with Context Manager Protocol\n>>> con1.close()\n2017-12-25 21:39:56 DEBUG: Put connection back to pool(pool1)\n>>> with con1 as cur:\n\tcur.execute('select 1+1')\n\n1\n2017-12-25 21:40:25 DEBUG: Put connection back to pool(pool1)\n### We can see that the module maintain the pool appropriate when(and only when) we call the close() method or use the Context Manager Protocol of connection object.\n```\n\n**NOTE 1:** We should always use one of the close() method or Context Manager Protocol of connection object, otherwise the pool will exhaust soon. \n**NOTE 2:** The Context Manager Protocol is preferred, it can achieve the \"multiplexing\" similar effect. \n**NOTE 3:** When use close() method, take care never use a connection object's close() method more than one time(you know why~).\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jkklee/pymysql-pool", "keywords": "pymysql pool,mysql connection pool,mysql multi threads", "license": "GPLv3", "maintainer": "", "maintainer_email": "", "name": "pymysql-pool", "package_url": "https://pypi.org/project/pymysql-pool/", "platform": "", "project_url": "https://pypi.org/project/pymysql-pool/", "project_urls": { "Homepage": "https://github.com/jkklee/pymysql-pool" }, "release_url": "https://pypi.org/project/pymysql-pool/0.3.3/", "requires_dist": [ "pymysql (>=0.7.10)" ], "requires_python": ">=3.4", "summary": "MySQL connection pool based pymysql", "version": "0.3.3" }, "last_serial": 5688370, "releases": { "0.3.1": [ { "comment_text": "", "digests": { "md5": "f1e0a8a771d92498f9d966e081016484", "sha256": "911fdf66d5ef442644fefa92a7d5b2e212542717b9321bf6bf92d5bd53f0d78f" }, "downloads": -1, "filename": "pymysql_pool-0.3.1-py3-none-any.whl", "has_sig": false, "md5_digest": "f1e0a8a771d92498f9d966e081016484", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 17703, "upload_time": "2018-12-10T14:35:46", "url": "https://files.pythonhosted.org/packages/9e/30/7e2669a63ae8cac3305868a44f31c4df8e72f34702d0dd60f3aed4fccb6a/pymysql_pool-0.3.1-py3-none-any.whl" } ], "0.3.2": [ { "comment_text": "", "digests": { "md5": "1ddbc20fa442ce03bb6bb13805275d0c", "sha256": "dc25890f65232fcabe43ee996ea0df4e3955fd8e47b8a38dd2fc73a01651eaca" }, "downloads": -1, "filename": "pymysql_pool-0.3.2-py3-none-any.whl", "has_sig": false, "md5_digest": "1ddbc20fa442ce03bb6bb13805275d0c", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 17709, "upload_time": "2018-12-10T15:10:58", "url": "https://files.pythonhosted.org/packages/3d/c8/0d2dfaa35bef579be046d5988963b781857819eec3f09022dbc8d45b664b/pymysql_pool-0.3.2-py3-none-any.whl" } ], "0.3.3": [ { "comment_text": "", "digests": { "md5": "e938a8e585950f01c233804ca642de3b", "sha256": "82338d14d7c89023a89c0703d4f6fd52113265be213813dfa3375642ee8c34cd" }, "downloads": -1, "filename": "pymysql_pool-0.3.3-py3-none-any.whl", "has_sig": false, "md5_digest": "e938a8e585950f01c233804ca642de3b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 17737, "upload_time": "2019-08-16T16:04:59", "url": "https://files.pythonhosted.org/packages/bd/f9/10a2369925f585e684aaa8c597992cc9d9e9394971640eac8566e6309b09/pymysql_pool-0.3.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "86da0d0d688ee6f3df677dade75c56fd", "sha256": "5b5e5b495927fa4ccff6c93b50751941679f26a64a222d55e6ad1163da659b78" }, "downloads": -1, "filename": "pymysql-pool-0.3.3.tar.gz", "has_sig": false, "md5_digest": "86da0d0d688ee6f3df677dade75c56fd", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 5036, "upload_time": "2019-08-16T16:05:02", "url": "https://files.pythonhosted.org/packages/b8/ec/9cea6c9d95819c7449ebf332f074933f416561e2cffc6a00e3a2764d2088/pymysql-pool-0.3.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e938a8e585950f01c233804ca642de3b", "sha256": "82338d14d7c89023a89c0703d4f6fd52113265be213813dfa3375642ee8c34cd" }, "downloads": -1, "filename": "pymysql_pool-0.3.3-py3-none-any.whl", "has_sig": false, "md5_digest": "e938a8e585950f01c233804ca642de3b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 17737, "upload_time": "2019-08-16T16:04:59", "url": "https://files.pythonhosted.org/packages/bd/f9/10a2369925f585e684aaa8c597992cc9d9e9394971640eac8566e6309b09/pymysql_pool-0.3.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "86da0d0d688ee6f3df677dade75c56fd", "sha256": "5b5e5b495927fa4ccff6c93b50751941679f26a64a222d55e6ad1163da659b78" }, "downloads": -1, "filename": "pymysql-pool-0.3.3.tar.gz", "has_sig": false, "md5_digest": "86da0d0d688ee6f3df677dade75c56fd", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 5036, "upload_time": "2019-08-16T16:05:02", "url": "https://files.pythonhosted.org/packages/b8/ec/9cea6c9d95819c7449ebf332f074933f416561e2cffc6a00e3a2764d2088/pymysql-pool-0.3.3.tar.gz" } ] }