|
| 1 | +<!DOCTYPE html> |
| 2 | +<html class="writer-html5" lang="en" > |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <link rel="shortcut icon" href="../img/favicon.ico" /> |
| 8 | + <title>Concurrent sql queries - context-async-sqlalchemy</title> |
| 9 | + <link rel="stylesheet" href="../css/theme.css" /> |
| 10 | + <link rel="stylesheet" href="../css/theme_extra.css" /> |
| 11 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" /> |
| 12 | + |
| 13 | + <script> |
| 14 | + // Current page data |
| 15 | + var mkdocs_page_name = "Concurrent sql queries"; |
| 16 | + var mkdocs_page_input_path = "concurrent_queries.md"; |
| 17 | + var mkdocs_page_url = null; |
| 18 | + </script> |
| 19 | + |
| 20 | + <!--[if lt IE 9]> |
| 21 | + <script src="../js/html5shiv.min.js"></script> |
| 22 | + <![endif]--> |
| 23 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script> |
| 24 | + <script>hljs.highlightAll();</script> |
| 25 | +</head> |
| 26 | + |
| 27 | +<body class="wy-body-for-nav" role="document"> |
| 28 | + |
| 29 | + <div class="wy-grid-for-nav"> |
| 30 | + <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav"> |
| 31 | + <div class="wy-side-scroll"> |
| 32 | + <div class="wy-side-nav-search"> |
| 33 | + <a href=".." class="icon icon-home"> context-async-sqlalchemy |
| 34 | + </a><div role="search"> |
| 35 | + <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get"> |
| 36 | + <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" title="Type search term here" /> |
| 37 | + </form> |
| 38 | +</div> |
| 39 | + </div> |
| 40 | + |
| 41 | + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> |
| 42 | + <ul> |
| 43 | + <li class="toctree-l1"><a class="reference internal" href="..">context-async-sqlalchemy</a> |
| 44 | + </li> |
| 45 | + </ul> |
| 46 | + <ul> |
| 47 | + <li class="toctree-l1"><a class="reference internal" href="../problem">What problems it solves</a> |
| 48 | + </li> |
| 49 | + </ul> |
| 50 | + <ul> |
| 51 | + <li class="toctree-l1"><a class="reference internal" href="../getting_started">Getting started</a> |
| 52 | + </li> |
| 53 | + </ul> |
| 54 | + <ul> |
| 55 | + <li class="toctree-l1"><a class="reference internal" href="../examples">Usage examples</a> |
| 56 | + </li> |
| 57 | + </ul> |
| 58 | + <ul class="current"> |
| 59 | + <li class="toctree-l1 current"><a class="reference internal current" href="#">Concurrent sql queries</a> |
| 60 | + <ul class="current"> |
| 61 | + </ul> |
| 62 | + </li> |
| 63 | + </ul> |
| 64 | + <ul> |
| 65 | + <li class="toctree-l1"><a class="reference internal" href="../master_replica">Master/Replica or several databases at the same time</a> |
| 66 | + </li> |
| 67 | + </ul> |
| 68 | + <ul> |
| 69 | + <li class="toctree-l1"><a class="reference internal" href="../testing">Testing</a> |
| 70 | + </li> |
| 71 | + </ul> |
| 72 | + <ul> |
| 73 | + <li class="toctree-l1"><a class="reference internal" href="../how_middleware_works">How middleware works</a> |
| 74 | + </li> |
| 75 | + </ul> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </nav> |
| 79 | + |
| 80 | + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| 81 | + <nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu"> |
| 82 | + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| 83 | + <a href="..">context-async-sqlalchemy</a> |
| 84 | + |
| 85 | + </nav> |
| 86 | + <div class="wy-nav-content"> |
| 87 | + <div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation"> |
| 88 | + <ul class="wy-breadcrumbs"> |
| 89 | + <li><a href=".." class="icon icon-home" aria-label="Docs"></a></li> |
| 90 | + <li class="breadcrumb-item active">Concurrent sql queries</li> |
| 91 | + </ul> |
| 92 | + <hr/> |
| 93 | +</div> |
| 94 | + <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| 95 | + <div class="section" itemprop="articleBody"> |
| 96 | + |
| 97 | + <h1 id="concurrent-sql-queries">Concurrent sql queries</h1> |
| 98 | +<p>Concurrent query execution deserves special attention. In sqlalchemy, |
| 99 | +you cannot concurrently run multiple queries within the same session. |
| 100 | +You need to create a new session. The library provides 2 ways to execute |
| 101 | +queries concurrently very easily:</p> |
| 102 | +<ul> |
| 103 | +<li>calling a function with a new context - <code>run_in_new_ctx</code></li> |
| 104 | +<li>get a new session that doesn't fit into the context at all - |
| 105 | +<code>new_non_ctx_atomic_session</code> or <code>new_non_ctx_session</code> </li> |
| 106 | +</ul> |
| 107 | +<pre><code class="language-python">import asyncio |
| 108 | + |
| 109 | +from context_async_sqlalchemy import ( |
| 110 | + close_db_session, |
| 111 | + commit_db_session, |
| 112 | + db_session, |
| 113 | + new_non_ctx_atomic_session, |
| 114 | + new_non_ctx_session, |
| 115 | + run_in_new_ctx, |
| 116 | +) |
| 117 | +from sqlalchemy import insert |
| 118 | + |
| 119 | +from ..database import connection |
| 120 | +from ..models import ExampleTable |
| 121 | + |
| 122 | + |
| 123 | +async def handler_multiple_sessions() -> None: |
| 124 | + """ |
| 125 | + In some situations, you need to have multiple sessions running |
| 126 | + simultaneously. For example, to run several queries concurrently. |
| 127 | + |
| 128 | + You can also use these same techniques to create new sessions whenever you |
| 129 | + need them. Not necessarily just because of the concurrent processing. |
| 130 | + """ |
| 131 | + await asyncio.gather( |
| 132 | + _insert(), # context session |
| 133 | + run_in_new_ctx(_insert), # new context and session with autocommit |
| 134 | + run_in_new_ctx( # new context and session with manual commit |
| 135 | + _insert_manual, "example_multiple_sessions", |
| 136 | + ), |
| 137 | + _insert_non_ctx(), # new non context session |
| 138 | + _insert_non_ctx_manual(), # new non context session |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +async def _insert() -> None: |
| 143 | + session = await db_session(connection) |
| 144 | + stmt = insert(ExampleTable).values(text="example_multiple_sessions") |
| 145 | + await session.execute(stmt) |
| 146 | + |
| 147 | + |
| 148 | +async def _insert_manual(text: str) -> None: |
| 149 | + session = await db_session(connection) |
| 150 | + stmt = insert(ExampleTable).values(text=text) |
| 151 | + await session.execute(stmt) |
| 152 | + |
| 153 | + # You can manually commit the transaction if you want, but it is not |
| 154 | + # necessary |
| 155 | + await commit_db_session(connection) |
| 156 | + |
| 157 | + # You can manually close the session if you want, but it is not necessary |
| 158 | + await close_db_session(connection) |
| 159 | + |
| 160 | + |
| 161 | +async def _insert_non_ctx() -> None: |
| 162 | + """ |
| 163 | + You don't have to use the context to work with sessions at all |
| 164 | + """ |
| 165 | + async with new_non_ctx_atomic_session(connection) as session: |
| 166 | + stmt = insert(ExampleTable).values(text="example_multiple_sessions") |
| 167 | + await session.execute(stmt) |
| 168 | + |
| 169 | + |
| 170 | +async def _insert_non_ctx_manual() -> None: |
| 171 | + """ |
| 172 | + You don't have to use the context to work with sessions at all |
| 173 | + """ |
| 174 | + async with new_non_ctx_session(connection) as session: |
| 175 | + stmt = insert(ExampleTable).values(text="example_multiple_sessions") |
| 176 | + await session.execute(stmt) |
| 177 | + await session.commit() |
| 178 | +</code></pre> |
| 179 | + |
| 180 | + </div> |
| 181 | + </div><footer> |
| 182 | + <div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation"> |
| 183 | + <a href="../examples" class="btn btn-neutral float-left" title="Usage examples"><span class="icon icon-circle-arrow-left"></span> Previous</a> |
| 184 | + <a href="../master_replica" class="btn btn-neutral float-right" title="Master/Replica or several databases at the same time">Next <span class="icon icon-circle-arrow-right"></span></a> |
| 185 | + </div> |
| 186 | + |
| 187 | + <hr/> |
| 188 | + |
| 189 | + <div role="contentinfo"> |
| 190 | + <!-- Copyright etc --> |
| 191 | + </div> |
| 192 | + |
| 193 | + Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| 194 | +</footer> |
| 195 | + |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + |
| 199 | + </section> |
| 200 | + |
| 201 | + </div> |
| 202 | + |
| 203 | + <div class="rst-versions" role="note" aria-label="Versions"> |
| 204 | + <span class="rst-current-version" data-toggle="rst-current-version"> |
| 205 | + |
| 206 | + <span> |
| 207 | + <a href="https://github.com/krylosov-aa/context-async-sqlalchemy" class="fa fa-github" style="color: #fcfcfc"> GitHub</a> |
| 208 | + </span> |
| 209 | + |
| 210 | + |
| 211 | + <span><a href="../examples" style="color: #fcfcfc">« Previous</a></span> |
| 212 | + |
| 213 | + |
| 214 | + <span><a href="../master_replica" style="color: #fcfcfc">Next »</a></span> |
| 215 | + |
| 216 | + </span> |
| 217 | +</div> |
| 218 | + <script src="../js/jquery-3.6.0.min.js"></script> |
| 219 | + <script>var base_url = "..";</script> |
| 220 | + <script src="../js/theme_extra.js"></script> |
| 221 | + <script src="../js/theme.js"></script> |
| 222 | + <script src="../search/main.js"></script> |
| 223 | + <script> |
| 224 | + jQuery(function () { |
| 225 | + SphinxRtdTheme.Navigation.enable(true); |
| 226 | + }); |
| 227 | + </script> |
| 228 | + |
| 229 | +</body> |
| 230 | +</html> |
0 commit comments