Skip to content

Commit ecc0df5

Browse files
committed
update docs
1 parent 42a1dde commit ecc0df5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+8960
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def some_func() -> None:
5959

6060
Here is a very simplified diagram of how everything works:
6161

62-
![basic schema.png](https://github.com/krylosov-aa/context-async-sqlalchemy/blob/main/docs_sources/docs/img/basic_schema.png)
62+
![basic schema.png](https://raw.githubusercontent.com/krylosov-aa/context-async-sqlalchemy/refs/heads/main/docs_sources/docs/img/basic_schema.png)
6363

6464
1. Before executing your code, the middleware will prepare a container in
6565
which the sessions required by your code will be stored.

docs/404.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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>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+
<!--[if lt IE 9]>
14+
<script src="/js/html5shiv.min.js"></script>
15+
<![endif]-->
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
17+
<script>hljs.highlightAll();</script>
18+
</head>
19+
20+
<body class="wy-body-for-nav" role="document">
21+
22+
<div class="wy-grid-for-nav">
23+
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
24+
<div class="wy-side-scroll">
25+
<div class="wy-side-nav-search">
26+
<a href="/." class="icon icon-home"> context-async-sqlalchemy
27+
</a><div role="search">
28+
<form id ="rtd-search-form" class="wy-form" action="//search.html" method="get">
29+
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" title="Type search term here" />
30+
</form>
31+
</div>
32+
</div>
33+
34+
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
35+
<ul>
36+
<li class="toctree-l1"><a class="reference internal" href="/.">context-async-sqlalchemy</a>
37+
</li>
38+
</ul>
39+
<ul>
40+
<li class="toctree-l1"><a class="reference internal" href="/problem/">What problems it solves</a>
41+
</li>
42+
</ul>
43+
<ul>
44+
<li class="toctree-l1"><a class="reference internal" href="/getting_started/">Getting started</a>
45+
</li>
46+
</ul>
47+
<ul>
48+
<li class="toctree-l1"><a class="reference internal" href="/examples">Usage examples</a>
49+
</li>
50+
</ul>
51+
<ul>
52+
<li class="toctree-l1"><a class="reference internal" href="/concurrent_queries/">Concurrent sql queries</a>
53+
</li>
54+
</ul>
55+
<ul>
56+
<li class="toctree-l1"><a class="reference internal" href="/master_replica/">Master/Replica or several databases at the same time</a>
57+
</li>
58+
</ul>
59+
<ul>
60+
<li class="toctree-l1"><a class="reference internal" href="/testing/">Testing</a>
61+
</li>
62+
</ul>
63+
<ul>
64+
<li class="toctree-l1"><a class="reference internal" href="/how_middleware_works/">How middleware works</a>
65+
</li>
66+
</ul>
67+
</div>
68+
</div>
69+
</nav>
70+
71+
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
72+
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
73+
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
74+
<a href="/.">context-async-sqlalchemy</a>
75+
76+
</nav>
77+
<div class="wy-nav-content">
78+
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
79+
<ul class="wy-breadcrumbs">
80+
<li><a href="/." class="icon icon-home" aria-label="Docs"></a></li>
81+
<li class="wy-breadcrumbs-aside">
82+
</li>
83+
</ul>
84+
<hr/>
85+
</div>
86+
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
87+
<div class="section" itemprop="articleBody">
88+
89+
90+
<h1 id="404-page-not-found">404</h1>
91+
92+
<p><strong>Page not found</strong></p>
93+
94+
95+
</div>
96+
</div><footer>
97+
98+
<hr/>
99+
100+
<div role="contentinfo">
101+
<!-- Copyright etc -->
102+
</div>
103+
104+
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>.
105+
</footer>
106+
107+
</div>
108+
</div>
109+
110+
</section>
111+
112+
</div>
113+
114+
<div class="rst-versions" role="note" aria-label="Versions">
115+
<span class="rst-current-version" data-toggle="rst-current-version">
116+
117+
<span>
118+
<a href="https://github.com/krylosov-aa/context-async-sqlalchemy" class="fa fa-github" style="color: #fcfcfc"> GitHub</a>
119+
</span>
120+
121+
122+
123+
</span>
124+
</div>
125+
<script src="/js/jquery-3.6.0.min.js"></script>
126+
<script>var base_url = "/";</script>
127+
<script src="/js/theme_extra.js"></script>
128+
<script src="/js/theme.js"></script>
129+
<script src="/search/main.js"></script>
130+
<script>
131+
jQuery(function () {
132+
SphinxRtdTheme.Navigation.enable(true);
133+
});
134+
</script>
135+
136+
</body>
137+
</html>

docs/concurrent_queries/index.html

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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() -&gt; None:
124+
&quot;&quot;&quot;
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+
&quot;&quot;&quot;
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, &quot;example_multiple_sessions&quot;,
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() -&gt; None:
143+
session = await db_session(connection)
144+
stmt = insert(ExampleTable).values(text=&quot;example_multiple_sessions&quot;)
145+
await session.execute(stmt)
146+
147+
148+
async def _insert_manual(text: str) -&gt; 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() -&gt; None:
162+
&quot;&quot;&quot;
163+
You don't have to use the context to work with sessions at all
164+
&quot;&quot;&quot;
165+
async with new_non_ctx_atomic_session(connection) as session:
166+
stmt = insert(ExampleTable).values(text=&quot;example_multiple_sessions&quot;)
167+
await session.execute(stmt)
168+
169+
170+
async def _insert_non_ctx_manual() -&gt; None:
171+
&quot;&quot;&quot;
172+
You don't have to use the context to work with sessions at all
173+
&quot;&quot;&quot;
174+
async with new_non_ctx_session(connection) as session:
175+
stmt = insert(ExampleTable).values(text=&quot;example_multiple_sessions&quot;)
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">&laquo; Previous</a></span>
212+
213+
214+
<span><a href="../master_replica" style="color: #fcfcfc">Next &raquo;</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>
85.6 KB
Binary file not shown.
65.7 KB
Binary file not shown.
84.3 KB
Binary file not shown.
64.9 KB
Binary file not shown.
162 KB
Binary file not shown.

0 commit comments

Comments
 (0)