├── .github ├── FUNDING.yml └── workflows │ ├── ci_psycopg2.yml │ ├── ci_psycopg2_gis.yml │ ├── ci_psycopg3.yml │ ├── ci_psycopg3_gis.yml │ └── python-publish.yml ├── .gitignore ├── AUTHORS.rst ├── CHANGELOG.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── django_db_geventpool ├── __init__.py ├── backends │ ├── __init__.py │ ├── base.py │ ├── creation.py │ ├── pool.py │ ├── postgis │ │ ├── __init__.py │ │ └── base.py │ ├── postgresql_psycopg2 │ │ ├── __init__.py │ │ └── base.py │ └── postgresql_psycopg3 │ │ ├── __init__.py │ │ └── base.py └── utils.py ├── docker-compose.yml ├── pyproject.toml ├── runtests_psycopg2.py ├── runtests_psycopg2_gis.py ├── runtests_psycopg3.py ├── runtests_psycopg3_gis.py ├── tests ├── __init__.py ├── models.py └── tests.py └── tox.ini /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: jneight 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | polar: # Replace with a single Polar username 13 | buy_me_a_coffee: # Replace with a single Buy Me a Coffee username 14 | thanks_dev: # Replace with a single thanks.dev username 15 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 16 | -------------------------------------------------------------------------------- /.github/workflows/ci_psycopg2.yml: -------------------------------------------------------------------------------- 1 | name: CI-psycopg2 2 | 'on': 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | env: 12 | POSTGRES_USER: postgres 13 | PGPASSWORD: postgres 14 | runs-on: '${{ matrix.os }}' 15 | strategy: 16 | matrix: 17 | include: 18 | - os: ubuntu-latest 19 | python-version: '3.8' 20 | django-version: '4.2.9' 21 | - os: ubuntu-latest 22 | python-version: '3.9' 23 | django-version: '4.2.9' 24 | - os: ubuntu-latest 25 | python-version: '3.10' 26 | django-version: '4.2.9' 27 | - os: ubuntu-latest 28 | python-version: '3.11' 29 | django-version: '4.2.9' 30 | - os: ubuntu-latest 31 | python-version: '3.12' 32 | django-version: '4.2.9' 33 | - os: ubuntu-latest 34 | python-version: '3.10' 35 | django-version: '5.0.8' 36 | - os: ubuntu-latest 37 | python-version: '3.11' 38 | django-version: '5.0.8' 39 | - os: ubuntu-latest 40 | python-version: '3.12' 41 | django-version: '5.0.8' 42 | - os: ubuntu-latest 43 | python-version: '3.10' 44 | django-version: '5.1' 45 | - os: ubuntu-latest 46 | python-version: '3.11' 47 | django-version: '5.1' 48 | - os: ubuntu-latest 49 | python-version: '3.12' 50 | django-version: '5.1' 51 | - os: ubuntu-latest 52 | python-version: '3.13' 53 | django-version: '5.1' 54 | services: 55 | postgres: 56 | image: postgres 57 | env: 58 | POSTGRES_USER: postgres 59 | POSTGRES_PASSWORD: postgres 60 | options: >- 61 | --health-cmd pg_isready 62 | --health-interval 10s 63 | --health-timeout 5s 64 | --health-retries 5 65 | ports: 66 | - 5432:5432 67 | steps: 68 | - name: 'Set up Python ${{ matrix.python-version }}' 69 | uses: actions/setup-python@v5 70 | with: 71 | python-version: '${{ matrix.python-version }}' 72 | - uses: actions/checkout@v4 73 | - run: pip install django==${{ matrix.django-version}} 74 | - run: pip install psycopg2 75 | - run: pip install psycogreen 76 | - run: pip install gevent 77 | - run: pip install . 78 | - run: python runtests_psycopg2.py 79 | -------------------------------------------------------------------------------- /.github/workflows/ci_psycopg2_gis.yml: -------------------------------------------------------------------------------- 1 | name: CI-psycopg2-gis 2 | 'on': 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | env: 12 | POSTGRES_USER: postgres 13 | PGPASSWORD: postgres 14 | runs-on: '${{ matrix.os }}' 15 | strategy: 16 | matrix: 17 | include: 18 | - os: ubuntu-latest 19 | python-version: '3.8' 20 | django-version: '4.2.9' 21 | - os: ubuntu-latest 22 | python-version: '3.9' 23 | django-version: '4.2.9' 24 | - os: ubuntu-latest 25 | python-version: '3.10' 26 | django-version: '4.2.9' 27 | - os: ubuntu-latest 28 | python-version: '3.11' 29 | django-version: '4.2.9' 30 | - os: ubuntu-latest 31 | python-version: '3.12' 32 | django-version: '4.2.9' 33 | - os: ubuntu-latest 34 | python-version: '3.10' 35 | django-version: '5.0.8' 36 | - os: ubuntu-latest 37 | python-version: '3.11' 38 | django-version: '5.0.8' 39 | - os: ubuntu-latest 40 | python-version: '3.12' 41 | django-version: '5.0.8' 42 | - os: ubuntu-latest 43 | python-version: '3.10' 44 | django-version: '5.1' 45 | - os: ubuntu-latest 46 | python-version: '3.11' 47 | django-version: '5.1' 48 | - os: ubuntu-latest 49 | python-version: '3.12' 50 | django-version: '5.1' 51 | - os: ubuntu-latest 52 | python-version: '3.13' 53 | django-version: '5.1' 54 | services: 55 | postgres: 56 | image: postgis/postgis 57 | env: 58 | POSTGRES_USER: postgres 59 | POSTGRES_PASSWORD: postgres 60 | options: >- 61 | --health-cmd pg_isready 62 | --health-interval 10s 63 | --health-timeout 5s 64 | --health-retries 5 65 | ports: 66 | - 5432:5432 67 | steps: 68 | - name: 'Set up Python ${{ matrix.python-version }}' 69 | uses: actions/setup-python@v5 70 | with: 71 | python-version: '${{ matrix.python-version }}' 72 | - uses: actions/checkout@v4 73 | - run: sudo apt-get update 74 | - run: sudo apt-get install gdal-bin libgdal-dev 75 | - run: pip install django==${{ matrix.django-version}} 76 | - run: pip install psycopg2 77 | - run: pip install psycogreen 78 | - run: pip install gevent 79 | - run: pip install gdal==3.4.1 80 | - run: pip install . 81 | - run: python runtests_psycopg2_gis.py 82 | -------------------------------------------------------------------------------- /.github/workflows/ci_psycopg3.yml: -------------------------------------------------------------------------------- 1 | name: CI-psycopg3 2 | 'on': 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | env: 12 | POSTGRES_USER: postgres 13 | PGPASSWORD: postgres 14 | runs-on: '${{ matrix.os }}' 15 | strategy: 16 | matrix: 17 | include: 18 | - os: ubuntu-latest 19 | python-version: '3.8' 20 | django-version: '4.2.9' 21 | - os: ubuntu-latest 22 | python-version: '3.9' 23 | django-version: '4.2.9' 24 | - os: ubuntu-latest 25 | python-version: '3.10' 26 | django-version: '4.2.9' 27 | - os: ubuntu-latest 28 | python-version: '3.11' 29 | django-version: '4.2.9' 30 | - os: ubuntu-latest 31 | python-version: '3.12' 32 | django-version: '4.2.9' 33 | - os: ubuntu-latest 34 | python-version: '3.10' 35 | django-version: '5.0.8' 36 | - os: ubuntu-latest 37 | python-version: '3.11' 38 | django-version: '5.0.8' 39 | - os: ubuntu-latest 40 | python-version: '3.12' 41 | django-version: '5.0.8' 42 | - os: ubuntu-latest 43 | python-version: '3.10' 44 | django-version: '5.1' 45 | - os: ubuntu-latest 46 | python-version: '3.11' 47 | django-version: '5.1' 48 | - os: ubuntu-latest 49 | python-version: '3.12' 50 | django-version: '5.1' 51 | - os: ubuntu-latest 52 | python-version: '3.13' 53 | django-version: '5.1' 54 | services: 55 | postgres: 56 | image: postgres 57 | env: 58 | POSTGRES_USER: postgres 59 | POSTGRES_PASSWORD: postgres 60 | options: >- 61 | --health-cmd pg_isready 62 | --health-interval 10s 63 | --health-timeout 5s 64 | --health-retries 5 65 | ports: 66 | - 5432:5432 67 | steps: 68 | - name: 'Set up Python ${{ matrix.python-version }}' 69 | uses: actions/setup-python@v5 70 | with: 71 | python-version: '${{ matrix.python-version }}' 72 | - uses: actions/checkout@v4 73 | - run: pip install django==${{ matrix.django-version}} 74 | - run: pip install psycopg[binary] 75 | - run: pip install gevent 76 | - run: pip install . 77 | - run: python runtests_psycopg3.py 78 | -------------------------------------------------------------------------------- /.github/workflows/ci_psycopg3_gis.yml: -------------------------------------------------------------------------------- 1 | name: CI-psycopg3-gis 2 | 'on': 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | env: 12 | POSTGRES_USER: postgres 13 | PGPASSWORD: postgres 14 | runs-on: '${{ matrix.os }}' 15 | strategy: 16 | matrix: 17 | include: 18 | - os: ubuntu-latest 19 | python-version: '3.8' 20 | django-version: '4.2.9' 21 | - os: ubuntu-latest 22 | python-version: '3.9' 23 | django-version: '4.2.9' 24 | - os: ubuntu-latest 25 | python-version: '3.10' 26 | django-version: '4.2.9' 27 | - os: ubuntu-latest 28 | python-version: '3.11' 29 | django-version: '4.2.9' 30 | - os: ubuntu-latest 31 | python-version: '3.12' 32 | django-version: '4.2.9' 33 | - os: ubuntu-latest 34 | python-version: '3.10' 35 | django-version: '5.0.8' 36 | - os: ubuntu-latest 37 | python-version: '3.11' 38 | django-version: '5.0.8' 39 | - os: ubuntu-latest 40 | python-version: '3.12' 41 | django-version: '5.0.8' 42 | - os: ubuntu-latest 43 | python-version: '3.10' 44 | django-version: '5.1' 45 | - os: ubuntu-latest 46 | python-version: '3.11' 47 | django-version: '5.1' 48 | - os: ubuntu-latest 49 | python-version: '3.12' 50 | django-version: '5.1' 51 | - os: ubuntu-latest 52 | python-version: '3.13' 53 | django-version: '5.1' 54 | services: 55 | postgres: 56 | image: postgis/postgis 57 | env: 58 | POSTGRES_USER: postgres 59 | POSTGRES_PASSWORD: postgres 60 | options: >- 61 | --health-cmd pg_isready 62 | --health-interval 10s 63 | --health-timeout 5s 64 | --health-retries 5 65 | ports: 66 | - 5432:5432 67 | steps: 68 | - name: 'Set up Python ${{ matrix.python-version }}' 69 | uses: actions/setup-python@v5 70 | with: 71 | python-version: '${{ matrix.python-version }}' 72 | - uses: actions/checkout@v4 73 | - run: sudo apt-get update 74 | - run: sudo apt-get install gdal-bin libgdal-dev 75 | - run: pip install django==${{ matrix.django-version}} 76 | - run: pip install psycopg[binary] 77 | - run: pip install gevent 78 | - run: pip install gdal==3.4.1 79 | - run: pip install . 80 | - run: python runtests_psycopg3_gis.py 81 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | # For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Upload Python Package 10 | 11 | on: 12 | release: 13 | types: [published] 14 | 15 | permissions: 16 | contents: read 17 | 18 | jobs: 19 | deploy: 20 | 21 | runs-on: ubuntu-latest 22 | 23 | steps: 24 | - uses: actions/checkout@v4 25 | - name: Set up Python 26 | uses: actions/setup-python@v5 27 | with: 28 | python-version: '3.x' 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip 32 | pip install hatch 33 | - name: Build package 34 | run: hatch build 35 | - name: Publish package 36 | uses: pypa/gh-action-pypi-publish@release/v1 37 | with: 38 | user: __token__ 39 | password: ${{ secrets.PYPI_API_TOKEN }} 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.pot 3 | *.pyc 4 | *.egg 5 | *.egg-info 6 | build/ 7 | dist/ 8 | local_settings.py 9 | .ropeproject 10 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | Javier Cordero / jneight 2 | Rajiv Makhijani / rajivm -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 4.0.4 4 | 5 | - Fix package 6 | 7 | ## 4.0.3 8 | 9 | - Support for psycopg3 10 | 11 | ## 4.0.2 12 | 13 | - Fixed eventlet rlock using as context manager, issue #73, @StuBz211 14 | 15 | ## 4.0.1 16 | 17 | - Changed JSONField to come from `django.db.models` instead of the removed (as of Django 4.0) `django.contrib.postgres.fields`, @veracrux 18 | 19 | ## 4.0.0 20 | 21 | - Support for django 4.0 22 | 23 | ## 3.2.3 24 | 25 | - Fixed TypeError when using JSONField with Django 3.1, @bvallant 26 | 27 | ## 3.2.2 28 | 29 | - Add "wait" option to prevent more than MAX_CONNS being created at the same time, @bellini666 30 | 31 | ## 3.2.0 32 | 33 | - Removed support for django < 1.11 34 | - Updated previous deprecation warnings 35 | - Improved queue and connection handling, @coderanger 36 | 37 | ## 3.1.0 38 | 39 | - Correct connection cleanup after disabling autocommit with `transaction.setautocommit(False)` 40 | 41 | ## 3.0.1 42 | 43 | - Fix setup.py UnicodeDecodeError when installing with python 3.6 44 | 45 | ## 3.0.0 46 | 47 | - psycopg2 is not installed by default, @mattbriancon 48 | 49 | ## 2 50 | 51 | - Testing: Add support for Python 3 and Django 2, @stefankoegl 52 | - Fixed error with undefined attribute "closed_in_transaction", @bmunoz89 53 | - Working with django 1.11 LTS and 2.0 54 | 55 | ## Previous versions: 56 | 57 | - Proper error handling for integer types, @gxx 58 | - Ensure that close_connection property closes, even in the case of an error, @gxx 59 | - Compatible with Python 3, @sumitalp 60 | - Fixed self.pool must exist, @rajivm 61 | - Fixed Postgis pool connection, @rajivm 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE AUTHORS.rst README.md 2 | recursive-include django-db-geventpool * 3 | recursive-exclude * *.pyc *.swp 4 | prune django_db_geventpool/.ropeproject 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | django-db-geventpool 2 | ==================== 3 | 4 | [![CI](https://github.com/jneight/django-db-geventpool/actions/workflows/ci.yml/badge.svg)](https://github.com/jneight/django-db-geventpool/actions/workflows/ci.yml) 5 | 6 | [![pypi version](https://img.shields.io/pypi/v/django-db-geventpool.svg)](https://pypi.python.org/pypi/django-db-geventpool) 7 | 8 | [![pypi license](http://img.shields.io/pypi/l/django-db-geventpool.svg)](https://pypi.python.org/pypi/django-db-geventpool) 9 | 10 | Another DB pool using gevent for PostgreSQL DB. 11 | 12 | 13 | psycopg3 14 | --------- 15 | 16 | Django, since 4.2, supports psycopg3. One of the advantages is that gevent is supported without needing extra patches, just install the package 17 | 18 | ``` 19 | $ pip install psycopg[binary] 20 | ``` 21 | 22 | 23 | psycopg2 24 | -------- 25 | 26 | If **gevent** is not installed, the pool will use **eventlet** as fallback. 27 | 28 | - `psycopg2>=2.5.1` for CPython 2 and 3 (or 29 | [psycopg2-binary](https://pypi.org/project/psycopg2-binary/)---see 30 | [notes in the psycopg2 2.7.4 31 | release](http://initd.org/psycopg/articles/2018/02/08/psycopg-274-released/)) 32 | - `psycopg2cffi>=2.7` for PyPy 33 | 34 | Patch psycopg2 35 | -------------- 36 | 37 | Before using the pool, psycopg2 must be patched with psycogreen, if you 38 | are using [gunicorn webserver](http://www.gunicorn.org/), a good place 39 | is the 40 | [post\_fork()](http://docs.gunicorn.org/en/latest/settings.html#post-fork) 41 | function at the config file: 42 | 43 | ``` {.python} 44 | from psycogreen.gevent import patch_psycopg # use this if you use gevent workers 45 | from psycogreen.eventlet import patch_psycopg # use this if you use eventlet workers 46 | 47 | def post_fork(server, worker): 48 | patch_psycopg() 49 | worker.log.info("Made Psycopg2 Green") 50 | ``` 51 | 52 | Settings 53 | -------- 54 | 55 | > - Set *ENGINE* in your database settings to: 56 | > 57 | > - For psycopg3: 'django_db_geventpool.backends.postgresql_psycopg3' 58 | > - For psycopg2: 'django_db_geventpool.backends.postgresql_psycopg2' 59 | > - For postgis: 'django_db_geventpool.backends.postgis' 60 | > 61 | > - Add *MAX\_CONNS* to *OPTIONS* to set the maximun number of 62 | > connections allowed to database (default=4) 63 | > 64 | > - Add *REUSE\_CONNS* to *OPTIONS* to indicate how many of the 65 | > MAX\_CONNS should be reused by new requests. Will fallback to the 66 | > same value as MAX\_CONNS if not defined 67 | > 68 | > - Add *\'CONN\_MAX\_AGE\': 0* to settings to disable default django 69 | > persistent connection feature. And read below note if you are 70 | > manually spawning greenlets 71 | 72 | For Django < 5.1: 73 | 74 | ``` {.python} 75 | DATABASES = { 76 | 'default': { 77 | 'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg', 78 | 'NAME': 'db', 79 | 'USER': 'postgres', 80 | 'PASSWORD': 'postgres', 81 | 'HOST': '', 82 | 'PORT': '', 83 | 'ATOMIC_REQUESTS': False, 84 | 'CONN_MAX_AGE': 0, 85 | 'OPTIONS': { 86 | 'MAX_CONNS': 20, 87 | 'REUSE_CONNS': 10 88 | } 89 | } 90 | } 91 | ``` 92 | 93 | For Django >= 5.1, native pool support should be disabled: 94 | 95 | ``` {.python} 96 | DATABASES = { 97 | 'default': { 98 | 'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg', 99 | 'NAME': 'db', 100 | 'USER': 'postgres', 101 | 'PASSWORD': 'postgres', 102 | 'HOST': '', 103 | 'PORT': '', 104 | 'ATOMIC_REQUESTS': False, 105 | 'CONN_MAX_AGE': 0, 106 | 'OPTIONS': { 107 | 'MAX_CONNS': 20, 108 | 'REUSE_CONNS': 10, 109 | 'pool': False 110 | } 111 | } 112 | } 113 | ``` 114 | 115 | Using ORM when not serving requests 116 | ----------------------------------- 117 | 118 | If you are using django with celery (or other), or have code that 119 | manually spawn greenlets it will not be sufficient to set CONN\_MAX\_AGE 120 | to 0. Django only checks for long-live connections when finishing a 121 | request - So if you manually spawn a greenlet (or task spawning one) its 122 | connections will not get cleaned up and will live until timeout. In 123 | production this can cause quite some open connections and while 124 | developing it can hamper your tests cases. 125 | 126 | To solve it make sure that each greenlet function (or task) either sends 127 | the django.core.signals.request\_finished signal or calls 128 | django.db.close\_old\_connections() right before it ends 129 | 130 | The decorator method with your function is preferred, but the other 131 | alternatives are also valid 132 | 133 | ``` {.python} 134 | from django_db_geventpool.utils import close_connection 135 | 136 | @close_connection 137 | def foo_func() 138 | ... 139 | ``` 140 | 141 | or 142 | 143 | ``` {.python} 144 | from django.core.signals import request_finished 145 | 146 | def foo_func(): 147 | ... 148 | request_finished.send(sender="greenlet") 149 | ``` 150 | 151 | or 152 | 153 | ``` {.python} 154 | from django.db import close_old_connections 155 | 156 | def foo_func(): 157 | ... 158 | close_old_connections() 159 | ``` 160 | 161 | Other pools 162 | ----------- 163 | 164 | - [django-db-pool](https://github.com/gmcguire/django-db-pool) 165 | - [django-postgresql](https://github.com/kennethreitz/django-postgrespool) 166 | -------------------------------------------------------------------------------- /django_db_geventpool/__init__.py: -------------------------------------------------------------------------------- 1 | version = 'v4.0.8' 2 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneight/django-db-geventpool/a6c9aab1bc0c8a415f3b55d7265153d5af3787e2/django_db_geventpool/backends/__init__.py -------------------------------------------------------------------------------- /django_db_geventpool/backends/base.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import sys 3 | 4 | try: 5 | from gevent.lock import Semaphore 6 | except ImportError: 7 | from eventlet.semaphore import Semaphore 8 | 9 | from .creation import DatabaseCreation 10 | 11 | logger = logging.getLogger("django.geventpool") 12 | 13 | connection_pools_lock = Semaphore(value=1) 14 | 15 | 16 | class DatabaseWrapperMixin: 17 | pool_class = None 18 | creation_class = DatabaseCreation 19 | INTRANS = None 20 | _connection_pools = {} 21 | 22 | 23 | def __init__(self, *args, **kwargs): 24 | self._pool = None 25 | super().__init__(*args, **kwargs) 26 | self.creation = self.creation_class(self) 27 | 28 | @property 29 | def pool(self): 30 | if self._pool is not None: 31 | return self._pool 32 | with connection_pools_lock: 33 | if self.alias not in self._connection_pools: 34 | self._pool = self.pool_class(**self.get_connection_params()) 35 | self._connection_pools[self.alias] = self._pool 36 | else: 37 | self._pool = self._connection_pools[self.alias] 38 | return self._pool 39 | 40 | def get_new_connection(self, conn_params: dict): 41 | if self.connection is None: 42 | self.connection = self.pool.get() 43 | self.closed_in_transaction = False 44 | return self.connection 45 | 46 | def get_connection_params(self) -> dict: 47 | conn_params = super().get_connection_params() 48 | for attr in ["MAX_CONNS", "REUSE_CONNS"]: 49 | if attr in self.settings_dict["OPTIONS"]: 50 | conn_params[attr] = self.settings_dict["OPTIONS"][attr] 51 | return conn_params 52 | 53 | def close(self): 54 | self.validate_thread_sharing() 55 | if self.closed_in_transaction or self.connection is None: 56 | return # no need to close anything 57 | try: 58 | self._close() 59 | except: 60 | # In some cases (database restart, network connection lost etc...) 61 | # the connection to the database is lost without giving Django a 62 | # notification. If we don't set self.connection to None, the error 63 | # will occur at every request. 64 | self.connection = None 65 | logger.warning( 66 | "psycopg error while closing the connection.", exc_info=sys.exc_info() 67 | ) 68 | raise 69 | finally: 70 | self.set_clean() 71 | 72 | def close_if_unusable_or_obsolete(self): 73 | # Always close the connection because it's not (usually) really being closed. 74 | self.close() 75 | 76 | def _close(self): 77 | if self.connection.closed: 78 | self.pool.close() 79 | else: 80 | if self.connection.info.transaction_status == self.INTRANS: 81 | self.connection.rollback() 82 | self.connection.autocommit = True 83 | with self.wrap_database_errors: 84 | self.pool.put(self.connection) 85 | self.connection = None 86 | 87 | def closeall(self): 88 | for pool in self._connection_pools.values(): 89 | pool.close() 90 | 91 | def set_clean(self): 92 | if self.in_atomic_block: 93 | self.closed_in_transaction = True 94 | self.needs_rollback = True 95 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/creation.py: -------------------------------------------------------------------------------- 1 | from django.db.backends.postgresql.creation import ( 2 | DatabaseCreation as OriginalDatabaseCreation, 3 | ) 4 | 5 | 6 | class DatabaseCreationMixin: 7 | def _create_test_db(self, verbosity, autoclobber, keepdb=False): 8 | return super()._create_test_db(verbosity, autoclobber, keepdb) 9 | 10 | def _destroy_test_db(self, test_database_name, verbosity): 11 | self.connection.closeall() 12 | return super()._destroy_test_db(test_database_name, verbosity) 13 | 14 | 15 | class DatabaseCreation(DatabaseCreationMixin, OriginalDatabaseCreation): 16 | pass 17 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/pool.py: -------------------------------------------------------------------------------- 1 | # this file is a modified version of the psycopg2 used at gevent examples 2 | # to be compatible with django, also checks if 3 | # DB connection is closed and reopen it: 4 | # https://github.com/surfly/gevent/blob/master/examples/psycopg2_pool.py 5 | import logging 6 | import weakref 7 | 8 | logger = logging.getLogger("django.geventpool") 9 | 10 | try: 11 | from gevent import queue 12 | from gevent.lock import RLock 13 | except ImportError: 14 | from eventlet import queue 15 | from ...utils import NullContextRLock as RLock 16 | 17 | 18 | class DatabaseConnectionPool: 19 | DBERROR = None 20 | 21 | def __init__(self, maxsize: int = 100, reuse: int = 100): 22 | # Use a WeakSet here so, even if we fail to discard the connection 23 | # when it is being closed, or it is closed outside of here, the item 24 | # will be removed automatically 25 | self._conns = weakref.WeakSet() 26 | self.maxsize = maxsize 27 | self.pool = queue.Queue(maxsize=max(reuse, 1)) 28 | self.lock = RLock() 29 | 30 | @property 31 | def size(self): 32 | with self.lock: 33 | return len(self._conns) 34 | 35 | def get(self): 36 | try: 37 | if self.size >= self.maxsize or self.pool.qsize(): 38 | conn = self.pool.get() 39 | else: 40 | conn = self.pool.get_nowait() 41 | 42 | try: 43 | # check connection is still valid 44 | self.check_usable(conn) 45 | logger.debug("DB connection reused") 46 | except self.DBERROR: 47 | logger.debug("DB connection was closed, creating a new one") 48 | conn = None 49 | except queue.Empty: 50 | conn = None 51 | logger.debug("DB connection queue empty, creating a new one") 52 | 53 | if conn is None: 54 | try: 55 | conn = self.create_connection() 56 | except Exception: 57 | raise 58 | else: 59 | self._conns.add(conn) 60 | 61 | return conn 62 | 63 | def put(self, item): 64 | try: 65 | self.pool.put_nowait(item) 66 | logger.debug("DB connection returned to the pool") 67 | except queue.Full: 68 | item.close() 69 | self._conns.discard(item) 70 | 71 | def close(self): 72 | while not self.pool.empty(): 73 | try: 74 | conn = self.pool.get_nowait() 75 | except queue.Empty: 76 | continue 77 | try: 78 | conn.close() 79 | except Exception: 80 | continue 81 | finally: 82 | self._conns.discard(conn) 83 | 84 | logger.debug("DB connections all closed") 85 | 86 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgis/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneight/django-db-geventpool/a6c9aab1bc0c8a415f3b55d7265153d5af3787e2/django_db_geventpool/backends/postgis/__init__.py -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgis/base.py: -------------------------------------------------------------------------------- 1 | from django.contrib.gis.db.backends.postgis.base import ( 2 | DatabaseWrapper as OriginalDatabaseWrapper, 3 | ) 4 | 5 | try: 6 | # try psycopg3 7 | import psycopg # noqa 8 | INTRANS = psycopg.pq.TransactionStatus.INTRANS 9 | from ..postgresql_psycopg3.base import base, PostgresConnectionPool 10 | except ImportError: 11 | # fallback to psycopg2 12 | import psycopg2 13 | INTRANS = psycopg2.extensions.TRANSACTION_STATUS_INTRANS 14 | from ..postgresql_psycopg2.base import base, PostgresConnectionPool 15 | 16 | 17 | class DatabaseWrapper(base.DatabaseWrapperMixin, OriginalDatabaseWrapper): 18 | pool_class = PostgresConnectionPool 19 | INTRANS = INTRANS 20 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgresql_psycopg2/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneight/django-db-geventpool/a6c9aab1bc0c8a415f3b55d7265153d5af3787e2/django_db_geventpool/backends/postgresql_psycopg2/__init__.py -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgresql_psycopg2/base.py: -------------------------------------------------------------------------------- 1 | try: 2 | import psycopg2 3 | import psycopg2.extras 4 | import psycopg2.extensions 5 | except ImportError as e: 6 | from django.core.exceptions import ImproperlyConfigured 7 | 8 | raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) 9 | 10 | from django.db.backends.postgresql.base import ( 11 | DatabaseWrapper as OriginalDatabaseWrapper, 12 | ) 13 | 14 | from .. import base, pool 15 | 16 | 17 | class PostgresConnectionPool(pool.DatabaseConnectionPool): 18 | DBERROR = psycopg2.DatabaseError 19 | 20 | def __init__(self, *args, **kwargs): 21 | self.connect = kwargs.pop("connect", psycopg2.connect) 22 | self.connection = None 23 | maxsize = kwargs.pop("MAX_CONNS", 4) 24 | reuse = kwargs.pop("REUSE_CONNS", maxsize) 25 | self.args = args 26 | self.kwargs = kwargs 27 | self.kwargs["client_encoding"] = "UTF8" 28 | super().__init__(maxsize, reuse) 29 | 30 | def create_connection(self): 31 | conn = self.connect(*self.args, **self.kwargs) 32 | psycopg2.extras.register_default_jsonb(conn_or_curs=conn, loads=lambda x: x) 33 | return conn 34 | 35 | def check_usable(self, connection): 36 | connection.cursor().execute("SELECT 1") 37 | 38 | 39 | class DatabaseWrapper(base.DatabaseWrapperMixin, OriginalDatabaseWrapper): 40 | pool_class = PostgresConnectionPool 41 | INTRANS = psycopg2.extensions.TRANSACTION_STATUS_INTRANS 42 | -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgresql_psycopg3/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneight/django-db-geventpool/a6c9aab1bc0c8a415f3b55d7265153d5af3787e2/django_db_geventpool/backends/postgresql_psycopg3/__init__.py -------------------------------------------------------------------------------- /django_db_geventpool/backends/postgresql_psycopg3/base.py: -------------------------------------------------------------------------------- 1 | try: 2 | import psycopg 3 | except ImportError as e: 4 | from django.core.exceptions import ImproperlyConfigured 5 | 6 | raise ImproperlyConfigured("Error loading psycopg3 module: %s" % e) 7 | 8 | from django.db.backends.postgresql.base import ( 9 | DatabaseWrapper as OriginalDatabaseWrapper, 10 | ) 11 | 12 | from .. import base, pool 13 | 14 | 15 | class PostgresConnectionPool(pool.DatabaseConnectionPool): 16 | DBERROR = psycopg.DatabaseError 17 | 18 | def __init__(self, *args, **kwargs): 19 | self.connect = kwargs.pop("connect", psycopg.connect) 20 | self.connection = None 21 | maxsize = kwargs.pop("MAX_CONNS", 4) 22 | reuse = kwargs.pop("REUSE_CONNS", maxsize) 23 | self.args = args 24 | self.kwargs = kwargs 25 | self.kwargs["client_encoding"] = "UTF8" 26 | super().__init__(maxsize, reuse) 27 | 28 | def create_connection(self): 29 | conn = self.connect(*self.args, **self.kwargs) 30 | return conn 31 | 32 | def check_usable(self, connection): 33 | connection.cursor().execute("SELECT 1") 34 | 35 | 36 | class DatabaseWrapper(base.DatabaseWrapperMixin, OriginalDatabaseWrapper): 37 | pool_class = PostgresConnectionPool 38 | INTRANS = psycopg.pq.TransactionStatus.INTRANS 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /django_db_geventpool/utils.py: -------------------------------------------------------------------------------- 1 | from functools import wraps 2 | 3 | from django.core.signals import request_finished 4 | 5 | 6 | def close_connection(f): 7 | @wraps(f) 8 | def wrapper(*args, **kwargs): 9 | try: 10 | return f(*args, **kwargs) 11 | finally: 12 | request_finished.send(sender="greenlet") 13 | 14 | return wrapper 15 | 16 | 17 | class NullContextRLock: 18 | def __init__(self, enter_result=None): 19 | self._enter_result = enter_result 20 | 21 | def __enter__(self): 22 | return self._enter_result 23 | 24 | def __exit__(self, exc_type, exc_val, exc_tb): 25 | return None 26 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.9' 2 | 3 | services: 4 | postgres: 5 | image: postgis/postgis:13-3.2-alpine 6 | shm_size: 128mb 7 | environment: 8 | POSTGRES_USER: postgres 9 | POSTGRES_PASSWORD: postgres 10 | expose: 11 | - 5432 12 | ports: 13 | - '127.0.0.1:5432:5432' 14 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["hatchling"] 3 | build-backend = "hatchling.build" 4 | 5 | [project] 6 | name = "django-db-geventpool" 7 | dynamic = ["version"] 8 | description = "Add a DB connection pool using gevent to django" 9 | readme = "README.md" 10 | license = "Apache-2.0" 11 | authors = [ 12 | { name = "Javier Cordero Martinez", email = "j@jcmz.me" }, 13 | ] 14 | classifiers = [ 15 | "Environment :: Web Environment", 16 | "Framework :: Django", 17 | "Framework :: Django :: 3.2", 18 | "Framework :: Django :: 4.2", 19 | "Framework :: Django :: 5.0", 20 | "Intended Audience :: Developers", 21 | "License :: OSI Approved :: Apache Software License", 22 | "Operating System :: OS Independent", 23 | "Programming Language :: Python", 24 | "Programming Language :: Python :: 3", 25 | "Topic :: Software Development :: Libraries :: Application Frameworks", 26 | ] 27 | dependencies = [ 28 | "django>=4.2", 29 | ] 30 | 31 | [project.urls] 32 | Homepage = "https://github.com/jneight/django-db-geventpool" 33 | 34 | [tool.hatch.version] 35 | path = "django_db_geventpool/__init__.py" 36 | 37 | [tool.hatch.build.targets.sdist] 38 | include = [ 39 | "/django_db_geventpool", 40 | ] 41 | -------------------------------------------------------------------------------- /runtests_psycopg2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import gevent.monkey 4 | 5 | gevent.monkey.patch_all() 6 | 7 | try: 8 | from psycopg2cffi import compat 9 | 10 | compat.register() 11 | except ImportError: 12 | pass 13 | 14 | import psycogreen.gevent 15 | 16 | psycogreen.gevent.patch_psycopg() 17 | 18 | import django 19 | from django.conf import settings 20 | from django.test.runner import DiscoverRunner 21 | 22 | 23 | settings.configure( 24 | DEBUG=True, 25 | DATABASES={ 26 | "default": { 27 | "ENGINE": "django_db_geventpool.backends.postgresql_psycopg2", 28 | "NAME": "test", 29 | "USER": "postgres", 30 | "PASSWORD": "postgres", 31 | "ATOMIC_REQUESTS": False, 32 | "CONN_MAX_AGE": 0, 33 | "HOST": "localhost", 34 | } 35 | }, 36 | INSTALLED_APPS=( 37 | "tests", 38 | "django_db_geventpool", 39 | ), 40 | USE_TZ=True, 41 | ) 42 | django.setup() 43 | 44 | test_runner = DiscoverRunner(verbosity=2) 45 | 46 | failures = test_runner.run_tests(["tests.tests"]) 47 | if failures: 48 | sys.exit(failures) 49 | -------------------------------------------------------------------------------- /runtests_psycopg2_gis.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import gevent.monkey 4 | 5 | gevent.monkey.patch_all() 6 | 7 | try: 8 | from psycopg2cffi import compat 9 | 10 | compat.register() 11 | except ImportError: 12 | pass 13 | 14 | import psycogreen.gevent 15 | 16 | psycogreen.gevent.patch_psycopg() 17 | 18 | import django 19 | from django.conf import settings 20 | from django.test.runner import DiscoverRunner 21 | 22 | 23 | settings.configure( 24 | DEBUG=True, 25 | DATABASES={ 26 | "default": { 27 | "ENGINE": "django_db_geventpool.backends.postgis", 28 | "NAME": "test", 29 | "USER": "postgres", 30 | "PASSWORD": "postgres", 31 | "ATOMIC_REQUESTS": False, 32 | "CONN_MAX_AGE": 0, 33 | "HOST": "localhost", 34 | } 35 | }, 36 | INSTALLED_APPS=( 37 | "tests", 38 | "django_db_geventpool", 39 | ), 40 | USE_TZ=True, 41 | ) 42 | django.setup() 43 | 44 | test_runner = DiscoverRunner(verbosity=2) 45 | 46 | failures = test_runner.run_tests(["tests.tests"]) 47 | if failures: 48 | sys.exit(failures) 49 | -------------------------------------------------------------------------------- /runtests_psycopg3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import gevent.monkey 4 | from distutils.version import StrictVersion 5 | 6 | gevent.monkey.patch_all() 7 | 8 | import django 9 | from django.conf import settings 10 | from django.test.runner import DiscoverRunner 11 | 12 | 13 | settings.configure( 14 | DEBUG=True, 15 | DATABASES={ 16 | "default": { 17 | "ENGINE": "django_db_geventpool.backends.postgresql_psycopg3", 18 | "NAME": "test", 19 | "USER": "postgres", 20 | "PASSWORD": "postgres", 21 | "ATOMIC_REQUESTS": False, 22 | "CONN_MAX_AGE": 0, 23 | "HOST": "localhost", 24 | } 25 | }, 26 | INSTALLED_APPS=( 27 | "tests", 28 | "django_db_geventpool", 29 | ), 30 | USE_TZ=True, 31 | ) 32 | 33 | if StrictVersion(django.get_version()) >= StrictVersion('5.1.0'): 34 | settings.DATABASES['default']["OPTIONS"] = {"pool": False} 35 | 36 | django.setup() 37 | 38 | test_runner = DiscoverRunner(verbosity=2) 39 | 40 | failures = test_runner.run_tests(["tests.tests"]) 41 | if failures: 42 | sys.exit(failures) 43 | -------------------------------------------------------------------------------- /runtests_psycopg3_gis.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | import gevent.monkey 4 | from distutils.version import StrictVersion 5 | 6 | gevent.monkey.patch_all() 7 | 8 | import django 9 | from django.conf import settings 10 | from django.test.runner import DiscoverRunner 11 | 12 | 13 | settings.configure( 14 | DEBUG=True, 15 | DATABASES={ 16 | "default": { 17 | "ENGINE": "django_db_geventpool.backends.postgis", 18 | "NAME": "test", 19 | "USER": "postgres", 20 | "PASSWORD": "postgres", 21 | "ATOMIC_REQUESTS": False, 22 | "CONN_MAX_AGE": 0, 23 | "HOST": "localhost", 24 | } 25 | }, 26 | INSTALLED_APPS=( 27 | "tests", 28 | "django_db_geventpool", 29 | ), 30 | USE_TZ=True, 31 | ) 32 | 33 | if StrictVersion(django.get_version()) >= StrictVersion('5.1.0'): 34 | settings.DATABASES['default']["OPTIONS"] = {"pool": False} 35 | 36 | django.setup() 37 | 38 | test_runner = DiscoverRunner(verbosity=2) 39 | 40 | failures = test_runner.run_tests(["tests.tests"]) 41 | if failures: 42 | sys.exit(failures) 43 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneight/django-db-geventpool/a6c9aab1bc0c8a415f3b55d7265153d5af3787e2/tests/__init__.py -------------------------------------------------------------------------------- /tests/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class TestModel(models.Model): 5 | charfield = models.CharField(max_length=32, blank=True) 6 | jsonfield = models.JSONField(default=dict) 7 | 8 | def __str__(self): 9 | return str(self.pk) 10 | -------------------------------------------------------------------------------- /tests/tests.py: -------------------------------------------------------------------------------- 1 | import gevent 2 | 3 | from django.test import TestCase 4 | from django_db_geventpool.utils import close_connection 5 | 6 | from .models import TestModel 7 | 8 | 9 | @close_connection 10 | def test_multiple_connections(count): 11 | print("Test {0} starts".format(count)) 12 | for x in range(0, 20): 13 | assert not TestModel.objects.exists() 14 | print("Test {0} ends".format(count)) 15 | 16 | 17 | class ModelTest(TestCase): 18 | databases = {"default"} 19 | 20 | def test_model_save(self): 21 | data = { 22 | "charfield": "testing save", 23 | "jsonfield": {"test": "value"}, 24 | } 25 | pk = TestModel.objects.create(**data).pk 26 | 27 | obj = TestModel.objects.get(pk=pk) 28 | for key in data.keys(): 29 | self.assertEqual(data[key], getattr(obj, key)) 30 | 31 | def test_connections(self): 32 | greenlets = [] 33 | 34 | for x in range(0, 50): 35 | greenlets.append(gevent.spawn(test_multiple_connections, x)) 36 | gevent.joinall(greenlets, raise_error=True) 37 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [gh-actions] 2 | python = 3 | 3.8: py38 4 | 3.9: py39 5 | 3.10: py310 6 | 3.11: py311 7 | 3.12: py312 8 | 3.13: py313 9 | 10 | [tox] 11 | envlist = 12 | py3{8,9,10,11,12}-dj{42}-pg{2-gis,2} 13 | py3{8,9,10,11,12}-dj{42}-pg{3-gis,3} 14 | py3{10,11,12}-dj{50}-pg{2-gis,2,3-gis,3} 15 | py3{10,11,12,13}-dj{51}-pg{3-gis,3} 16 | 17 | [testenv] 18 | deps = 19 | gevent 20 | psycogreen 21 | dj42: django~=4.2 22 | dj50: django~=5.0 23 | dj51: django~=5.1 24 | pg2-gis: psycopg2-binary 25 | pg2: psycopg2-binary 26 | pg3-gis: psycopg[binary,pool] 27 | pg3: psycopg[binary,pool] 28 | 29 | commands = 30 | pg2-gis: python -Wall runtests_psycopg2_gis.py 31 | pg2: python -Wall runtests_psycopg2.py 32 | pg3-gis: python -Wall runtests_psycopg3_gis.py 33 | pg3: python -Wall runtests_psycopg3.py 34 | --------------------------------------------------------------------------------