<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>python on Saurabh Kumar</title><link>https://saurabh-kumar.com/tags/python/</link><description>Recent content in python on Saurabh Kumar</description><generator>Hugo</generator><language>en-US</language><copyright>Copyright © 2025, Saurabh Kumar.</copyright><lastBuildDate>Wed, 30 Sep 2026 12:00:00 +0000</lastBuildDate><atom:link href="https://saurabh-kumar.com/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>django-ox</title><link>https://saurabh-kumar.com/til/1790769600/</link><pubDate>Wed, 30 Sep 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1790769600/</guid><description>&lt;p&gt;&lt;a href="https://oxpull.com/django-ox/" rel="noopener external"&gt;django-ox&lt;/a&gt; makes the database the Django task queue: transactional enqueue inside &lt;code&gt;atomic()&lt;/code&gt;, no Redis/RabbitMQ, retries, worker-death recovery, recurring schedules, admin and metrics. Adoption read: pilot it for ordinary Django jobs; keep Celery for broker-scale workflows.&lt;/p&gt;&#10;</description></item><item><title>jetify-com/typeid</title><link>https://saurabh-kumar.com/til/1790424000/</link><pubDate>Sat, 26 Sep 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1790424000/</guid><description>&lt;p&gt;&lt;a href="https://github.com/jetify-com/typeid" rel="noopener external"&gt;jetify-com/typeid&lt;/a&gt; is a spec for Stripe-style IDs: &lt;code&gt;user_01h2xcejqtf2nbrexx3vqjhp41&lt;/code&gt; is a snake_case type prefix, &lt;code&gt;_&lt;/code&gt;, then a UUIDv7 in 26 chars of Crockford base32 (hex UUIDs take 36). Time-ordered, so B-tree inserts stay local, unlike UUIDv4. Double-click selects the whole ID, and a serializer expecting &lt;code&gt;user_&lt;/code&gt; rejects a pasted &lt;code&gt;post_&lt;/code&gt; ID. Strip the prefix and you get a valid UUIDv7 back, so the database never has to know. Cheap way to adopt: keep the column a native 16-byte &lt;code&gt;uuid&lt;/code&gt; (&lt;code&gt;DEFAULT uuidv7()&lt;/code&gt; on Postgres 18, &lt;code&gt;uuid.uuid7()&lt;/code&gt; on Python 3.14) and encode/decode only at the API edge, e.g. a DRF serializer field on top of &lt;a href="https://github.com/akhundMurad/typeid-python" rel="noopener external"&gt;typeid-python&lt;/a&gt;. If PKs are already &lt;code&gt;uuid&lt;/code&gt;, no schema change, and IDs explain themselves in logs and support tickets.&lt;/p&gt;&#10;</description></item><item><title>NVIDIA's NOOA framework</title><link>https://saurabh-kumar.com/til/1785240000/</link><pubDate>Tue, 28 Jul 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1785240000/</guid><description>&lt;p&gt;Python&amp;rsquo;s &lt;code&gt;Ellipsis&lt;/code&gt; literal (&lt;code&gt;...&lt;/code&gt;) is a real object (&lt;code&gt;type(...)&lt;/code&gt; is &lt;code&gt;&amp;lt;class 'ellipsis'&amp;gt;&lt;/code&gt;), not just a placeholder. Beyond type stubs and numpy slicing, &lt;a href="https://github.com/NVIDIA-NeMo/labs-OO-Agents" rel="noopener external"&gt;NVIDIA&amp;rsquo;s NOOA framework&lt;/a&gt; repurposes it as a runtime dispatch marker: methods with &lt;code&gt;...&lt;/code&gt; bodies are completed by an LLM at runtime, while methods with real bodies stay deterministic Python. The boundary between &amp;ldquo;code I control&amp;rdquo; and &amp;ldquo;code the model controls&amp;rdquo; becomes greppable: &lt;code&gt;grep -n '\.\.\.' agent.py&lt;/code&gt;. Clever reuse of existing syntax that linters and type checkers already accept.&lt;/p&gt;&#10;</description></item><item><title>django-crawl</title><link>https://saurabh-kumar.com/til/1784894401/</link><pubDate>Fri, 24 Jul 2026 12:00:01 +0000</pubDate><guid>https://saurabh-kumar.com/til/1784894401/</guid><description>&lt;p&gt;&lt;a href="https://django-crawl.readthedocs.io/en/latest/" rel="noopener external"&gt;django-crawl&lt;/a&gt; by Adam Johnson: BFS-crawls your Django site using the test client, no real HTTP overhead. &lt;a href="https://adamj.eu/tech/2026/07/22/introducing-django-crawl/" rel="noopener external"&gt;Origin story&lt;/a&gt;: diffing CSP headers during a Django 6.0 migration accidentally caught 7 non-CSP bugs in a 100%-coverage project. HTML parsing via Rust (html5ever), follows links/sitemaps/feeds. Works as a management command or a TestCase smoke test that raises &lt;code&gt;ExceptionGroup&lt;/code&gt; on errors.&lt;/p&gt;&#10;</description></item><item><title>import datetime as dt</title><link>https://saurabh-kumar.com/til/1783771200/</link><pubDate>Sat, 11 Jul 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1783771200/</guid><description>&lt;p&gt;Python 3.15 datetime docs now officially recommend &lt;a href="https://docs.python.org/3.15/library/datetime.html#module-datetime:~:text=Tip%20import%20datetime%20as%20dt" rel="noopener external"&gt;&lt;code&gt;import datetime as dt&lt;/code&gt;&lt;/a&gt; in a Tip block, to disambiguate the module from the &lt;code&gt;datetime.datetime&lt;/code&gt; class. The tip cites &lt;a href="https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/" rel="noopener external"&gt;Adam Johnson&amp;rsquo;s 2019 post&lt;/a&gt; — 6 years from community convention to CPython docs. Fixes both traps: &lt;code&gt;from datetime import datetime&lt;/code&gt; shadows the module, and &lt;code&gt;import datetime&lt;/code&gt; reads ambiguously as &lt;code&gt;datetime.datetime.now()&lt;/code&gt;. &lt;a href="https://fosstodon.org/@adamchainz/116897748723239892" rel="noopener external"&gt;via&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>Django ORM: From sync_to_async Threads to Native psycopg3</title><link>https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/</link><pubDate>Sat, 13 Jun 2026 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/</guid><description>&lt;p&gt;Django shipped async ORM methods back in 3.1. The &lt;code&gt;a&lt;/code&gt;-prefixed methods, including&#10;&lt;code&gt;aget()&lt;/code&gt;, &lt;code&gt;afilter()&lt;/code&gt;, and &lt;code&gt;acreate()&lt;/code&gt;, let calling code use coroutines, but the&#10;database work still blocked an OS thread. I want to explain that distinction&#10;and how the execution path changed.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 fixed it for real in December 2025. I want to walk through the path&#10;from &amp;ldquo;async via threads&amp;rdquo; to &amp;ldquo;native async via psycopg3,&amp;rdquo; because it is not&#10;a story about a feature landing. It is a story about three constraints that&#10;boxed Django in for five years: &lt;code&gt;psycopg2&lt;/code&gt; blocks, connection state lived in&#10;thread-locals, and the entire ORM assumed DB-API2. Once you see those three, every&#10;design decision Django made falls out of them almost mechanically.&lt;/p&gt;&#10;&lt;h2 id="the-thread-based-async-path"&gt;The Thread-Based Async Path&lt;/h2&gt;&#10;&lt;p&gt;When async views arrived in 3.1, the ORM underneath them was entirely synchronous.&#10;&lt;code&gt;psycopg2&lt;/code&gt; — the default PostgreSQL driver for most of Django&amp;rsquo;s life — wraps&#10;&lt;code&gt;libpq&lt;/code&gt;, a C library with no async I/O surface at all. There is no flag you set to&#10;make &lt;code&gt;psycopg2&lt;/code&gt; async. You either replace the driver or you find a workaround.&lt;/p&gt;&#10;&lt;p&gt;Django found the workaround: &lt;code&gt;asgiref.sync.sync_to_async&lt;/code&gt;.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Simplified — what aget() actually did under the hood in Django 5.x&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;asgiref.sync&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sync_to_async&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;aget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sync_to_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_sensitive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;sync_to_async&lt;/code&gt; pushes the callable into a &lt;code&gt;ThreadPoolExecutor&lt;/code&gt;. The event loop is&#10;not blocked, because the coroutine awaits the thread&amp;rsquo;s future. But a real OS thread&#10;is blocked, sitting on a TCP socket waiting for Postgres to answer. You moved the&#10;blocking, you did not remove it.&lt;/p&gt;&#10;&lt;p&gt;The &lt;code&gt;thread_sensitive=True&lt;/code&gt; flag is the part that gives the game away. Django&amp;rsquo;s&#10;database connections lived in thread-locals:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# django/db/backends/base/base.py (pre-6.0)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;threading&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_thread_local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without &lt;code&gt;thread_sensitive=True&lt;/code&gt;, two consecutive &lt;code&gt;sync_to_async&lt;/code&gt; calls from the same&#10;request might land on different threads, see different thread-locals, and tear your&#10;transaction state in half. The flag pins every call within the same coroutine&#10;context to one dedicated thread, so the connection still looks like a single&#10;connection per request. This preserves the connection’s thread affinity.&lt;/p&gt;&#10;&lt;p&gt;Put it together and the cost is obvious. Five hundred concurrent async requests, each&#10;touching the database, need five hundred threads. That is the thread-per-connection&#10;model, even though the calling code uses coroutines.&lt;/p&gt;&#10;&lt;h2 id="why-psycopg2-was-a-dead-end"&gt;Why psycopg2 Was a Dead End&lt;/h2&gt;&#10;&lt;p&gt;You cannot retrofit async I/O onto &lt;code&gt;psycopg2&lt;/code&gt;. It calls into &lt;code&gt;libpq&lt;/code&gt; synchronously&#10;and blocks the calling thread until the query returns. The only way to run it in an&#10;async context is exactly what Django did: park it in a thread and await that thread.&#10;There was never a clever escape here.&lt;/p&gt;&#10;&lt;p&gt;The obvious alternative was &lt;code&gt;asyncpg&lt;/code&gt;, a fully async PostgreSQL driver that beats&#10;psycopg3 on raw throughput. Django ruled it out, and the reasons are worth their own&#10;section further down. The short version is that &lt;code&gt;asyncpg&lt;/code&gt; does not implement DB-API2&#10;(PEP 249)&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, and Django&amp;rsquo;s ORM is built on DB-API2 from the ground up — cursor&#10;objects, &lt;code&gt;execute()&lt;/code&gt;, &lt;code&gt;fetchone()&lt;/code&gt;, &lt;code&gt;fetchall()&lt;/code&gt;. You do not bolt a different&#10;contract onto twenty years of ORM and call it a driver swap.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;psycopg3&lt;/code&gt; is the answer that fits, because it does not force that choice.&lt;/p&gt;&#10;&lt;h2 id="psycopg3-does-async-and-db-api2-at-the-same-time"&gt;psycopg3 Does Async And DB-API2 At The Same Time&lt;/h2&gt;&#10;&lt;p&gt;&lt;code&gt;psycopg3&lt;/code&gt; — the package is just &lt;code&gt;psycopg&lt;/code&gt;, not &lt;code&gt;psycopg3&lt;/code&gt; — was redesigned to support&#10;both synchronous and asynchronous execution through one shared core&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;. The same query&#10;compilation logic runs both paths. What changes is the connection and cursor type you&#10;hand it.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# psycopg3 — synchronous path&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;SELECT * FROM posts WHERE id = &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# psycopg3 — async path (same SQL, same parameters, different objects)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;aconn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;aconn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;SELECT * FROM posts WHERE id = &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;AsyncConnection&lt;/code&gt; and &lt;code&gt;AsyncCursor&lt;/code&gt; use &lt;code&gt;asyncio&lt;/code&gt; directly. The &lt;code&gt;await cur.execute(...)&lt;/code&gt;&#10;suspends the coroutine and releases the event loop while the network I/O happens. No&#10;thread sits idle on a socket. That is the whole difference, and it is the difference&#10;that matters.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 made psycopg3 the recommended PostgreSQL driver and wired&#10;&lt;code&gt;AsyncConnection&lt;/code&gt; and &lt;code&gt;AsyncCursor&lt;/code&gt; into the backend.&lt;/p&gt;&#10;&lt;h2 id="what-django-60-actually-changed"&gt;What Django 6.0 Actually Changed&lt;/h2&gt;&#10;&lt;p&gt;Three refactors enabled the switch. None of them are glamorous, and that is the point.&lt;/p&gt;&#10;&lt;h3 id="1-contextvar-replaces-thread-locals"&gt;1. ContextVar Replaces Thread-Locals&lt;/h3&gt;&#10;&lt;p&gt;Thread-locals are indexed by thread ID. Coroutines do not have thread IDs of their&#10;own — they share threads and switch between each other at &lt;code&gt;await&lt;/code&gt; points. So&#10;thread-local connection storage is not merely awkward under async, it is structurally&#10;wrong. Think of a thread-local as a coat hook bolted to a single chair: it works only&#10;as long as one person stays in that chair the whole time. Coroutines stand up and swap&#10;chairs constantly.&lt;/p&gt;&#10;&lt;p&gt;The fix is &lt;code&gt;contextvars.ContextVar&lt;/code&gt;, which arrived in Python 3.7. A &lt;code&gt;ContextVar&lt;/code&gt; is&#10;scoped to the current execution context, and each asyncio &lt;code&gt;Task&lt;/code&gt; gets its own context,&#10;copied from its parent when it is created. Django 6.0 moved connection storage off&#10;&lt;code&gt;threading.local()&lt;/code&gt; and onto a &lt;code&gt;ContextVar&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# django/db/backends/base/base.py (Django 6.0)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;contextvars&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;_connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ContextVar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;django_db_connection&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Two concurrent requests, both awaiting a query, each live in their own asyncio Task,&#10;each with their own context copy, each seeing their own connection. No locking, no&#10;thread affinity, no &lt;code&gt;thread_sensitive&lt;/code&gt; gymnastics. The coat hook now travels with the&#10;person.&lt;/p&gt;&#10;&lt;h3 id="2-databasewrapper-gets-an-async-connection-path"&gt;2. DatabaseWrapper Gets an Async Connection Path&lt;/h3&gt;&#10;&lt;p&gt;&lt;code&gt;DatabaseWrapper&lt;/code&gt; is the Django object that represents a database connection. Before&#10;6.0 it exposed one path, &lt;code&gt;self.connection&lt;/code&gt;, a synchronous psycopg2 connection. After&#10;6.0 it also exposes &lt;code&gt;async_connection()&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Conceptual — the async context manager on DatabaseWrapper&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@asynccontextmanager&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_connection_params&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;_connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Django&amp;rsquo;s connection-handling code detects whether it is running inside an asyncio event&#10;loop and routes to the right path. You do not pick the path. The runtime does.&lt;/p&gt;&#10;&lt;h3 id="3-sqlcompiler-gets-async_execute"&gt;3. SQLCompiler Gets async_execute&lt;/h3&gt;&#10;&lt;p&gt;The query compiler, &lt;code&gt;SQLCompiler&lt;/code&gt; and its subclasses, is where Django turns a &lt;code&gt;QuerySet&lt;/code&gt;&#10;into SQL and sends it to the database. Before 6.0 there was one path, &lt;code&gt;execute_sql()&lt;/code&gt;.&#10;Django 6.0 adds &lt;code&gt;async_execute_sql()&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_execute_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MULTI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunked_fetch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;GET_ITERATOR_CHUNK_SIZE&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_connection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_sql&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MULTI&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;aget()&lt;/code&gt;, &lt;code&gt;afilter()&lt;/code&gt;, &lt;code&gt;aiterator()&lt;/code&gt; and the rest now route through &lt;code&gt;async_execute_sql()&lt;/code&gt;&#10;instead of wrapping &lt;code&gt;execute_sql()&lt;/code&gt; in &lt;code&gt;sync_to_async&lt;/code&gt;. The thread pool drops out of the&#10;picture entirely.&lt;/p&gt;&#10;&lt;h2 id="the-new-execution-stack"&gt;The New Execution Stack&lt;/h2&gt;&#10;&lt;p&gt;Here is how &lt;code&gt;await Post.objects.aget(id=1)&lt;/code&gt; flows through Django 6.0:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;await Post.objects.aget(id=1)&#10; └── QuerySet.aget()&#10; └── SQLCompiler.async_execute_sql()&#10; └── DatabaseWrapper.async_connection() # ContextVar-scoped&#10; └── psycopg.AsyncConnection&#10; └── AsyncCursor.execute()&#10; └── asyncio TCP socket write&#10; └── await socket read ← event loop released here&#10; └── row data returned&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And here is the same call under Django 5.x:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;await Post.objects.aget(id=1)&#10; └── QuerySet.aget()&#10; └── sync_to_async(thread_sensitive=True)(QuerySet.get)&#10; └── ThreadPoolExecutor — OS thread blocked&#10; └── SQLCompiler.execute_sql()&#10; └── psycopg2 connection (thread-local)&#10; └── libpq blocking TCP call&#10; └── thread wakes, result returned&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The event loop is no longer handing work off to a thread. It suspends the coroutine,&#10;does other work, and resumes when the socket is readable. That is the entire reform,&#10;and it is enough.&lt;/p&gt;&#10;&lt;h2 id="what-this-means-for-concurrency"&gt;What This Means For Concurrency&lt;/h2&gt;&#10;&lt;p&gt;The practical payoff is in connection count. With the old thread approach, five hundred&#10;concurrent async requests meant up to five hundred threads and up to five hundred&#10;Postgres connections. Threads are cheap but not free — each costs roughly 8MB of stack&#10;by default, plus scheduler overhead.&lt;/p&gt;&#10;&lt;p&gt;With native async, those same five hundred requests can be multiplexed over a&#10;connection pool sized to what your database can actually handle, typically 10 to 50&#10;connections, with the event loop running the wait queue. You use far fewer threads,&#10;one or a handful for the event loop workers, and far fewer database connections.&lt;/p&gt;&#10;&lt;p&gt;Django 6.0 also ships native connection pooling for the async path&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;, built on&#10;&lt;code&gt;psycopg_pool.AsyncConnectionPool&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# settings.py — async pooling in Django 6.0&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;min_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the same &lt;code&gt;pool&lt;/code&gt; option I covered in &lt;a href="https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/"&gt;Django Native PostgreSQL Connection Pooling&lt;/a&gt;, with one&#10;difference that matters. In that post I warned against native pooling under ASGI —&#10;Django&amp;rsquo;s own docs recommended PgBouncer instead — because the sync pool and the event&#10;loop did not cooperate. Django 6.0&amp;rsquo;s &lt;code&gt;AsyncConnectionPool&lt;/code&gt; is what closes that gap. The&#10;pool itself is now async-aware, so the ASGI caveat no longer applies on the native&#10;psycopg3 path. I was right to warn you then. You can stop worrying about it now.&lt;/p&gt;&#10;&lt;h2 id="is-sync_to_async-gone"&gt;Is sync_to_async Gone?&lt;/h2&gt;&#10;&lt;p&gt;No, and it should not be. It is the fallback for drivers that cannot go native async:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Driver&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Async path&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;psycopg3 (PostgreSQL)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Native &lt;code&gt;AsyncCursor&lt;/code&gt; — no threads&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;psycopg2 (PostgreSQL)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap — still blocked&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;mysqlclient / MySQL&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;sqlite3&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;sync_to_async&lt;/code&gt; thread wrap&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Run Django 6.0 against &lt;code&gt;psycopg2&lt;/code&gt; or MySQL in an async context and Django still falls&#10;back to the thread pool. The native path only exists when psycopg3 is installed and&#10;configured. The thread trick did not become wrong. It became the path of last resort.&lt;/p&gt;&#10;&lt;p&gt;The way forward for other databases is psycopg3-equivalent async drivers. MySQL has&#10;&lt;code&gt;aiomysql&lt;/code&gt;, SQLite has &lt;code&gt;aiosqlite&lt;/code&gt;. Django contributions for those backends are in&#10;progress but not stable in 6.0, so for now PostgreSQL is the database that gets the&#10;real thing.&lt;/p&gt;&#10;&lt;h2 id="asyncpg-is-still-faster-and-still-not-supported"&gt;asyncpg Is Still Faster, And Still Not Supported&lt;/h2&gt;&#10;&lt;p&gt;Benchmark &lt;code&gt;asyncpg&lt;/code&gt; against psycopg3 on raw async throughput and asyncpg wins, often&#10;by 20 to 40 percent on bulk read workloads. Django will not adopt it. The reasons are&#10;structural, not political, and I think Django is right.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;No DB-API2.&lt;/strong&gt; asyncpg uses positional &lt;code&gt;$1, $2&lt;/code&gt; parameters, not &lt;code&gt;%s&lt;/code&gt;. The entire&#10;ORM query compiler assumes &lt;code&gt;%s&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;No synchronous fallback.&lt;/strong&gt; Management commands, migrations, and sync views would&#10;need a whole separate code path.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;A different transaction model.&lt;/strong&gt; asyncpg handles transactions differently from the&#10;DB-API2 autocommit and explicit model Django manages internally.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Using asyncpg would require changes to the ORM, not just a driver swap.&#10;A driver benchmark alone does not measure that work. If you genuinely need asyncpg throughput with a Django-like&#10;ORM, look at SQLAlchemy 2.0 with its async session support and asyncpg backend, running&#10;under Starlette or FastAPI. For Django, psycopg3 is the correct answer.&lt;/p&gt;&#10;&lt;h2 id="upgrading"&gt;Upgrading&lt;/h2&gt;&#10;&lt;p&gt;To get the native async path today:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pip install &lt;span class="s2"&gt;&amp;#34;psycopg[binary,pool]&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# settings.py&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;mydb&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;myuser&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;mypassword&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;localhost&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# or dict with min_size/max_size&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you are on Django 5.2 LTS with psycopg3, you already get the synchronous psycopg3&#10;path and connection pooling. The native async query path needs Django 6.0. You can&#10;upgrade the driver without upgrading Django and still get the pooling benefits in sync&#10;contexts, which is a fine thing to do while you wait.&lt;/p&gt;&#10;&lt;h2 id="why-it-took-five-years"&gt;Why It Took Five Years&lt;/h2&gt;&#10;&lt;p&gt;Django&amp;rsquo;s async ORM story ran from &amp;ldquo;async with threads&amp;rdquo; in 3.1 in 2020 to &amp;ldquo;native&#10;async with psycopg3&amp;rdquo; in 6.0 in 2025. Five years sounds slow until you look at what had&#10;to be true first:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;psycopg3&lt;/code&gt; had to be stable enough to depend on, which it reached around 2022 to 2023.&lt;/li&gt;&#10;&lt;li&gt;Thread-local connection state was woven through the codebase, and the ContextVar&#10;migration touched hundreds of files.&lt;/li&gt;&#10;&lt;li&gt;The Django project is conservative about breaking changes, especially in database&#10;handling, where bugs are not cosmetic.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The result is correct and maintainable rather than the absolute fastest thing money can&#10;buy, and I do not think that was the wrong trade. For most Django applications — web&#10;services handling thousands of concurrent HTTP requests — native async with psycopg3 is&#10;already more than fast enough, and it integrates cleanly with everything else in the&#10;ecosystem.&lt;/p&gt;&#10;&lt;h2 id="related-reading"&gt;Related reading&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/"&gt;Django Native PostgreSQL Connection Pooling&lt;/a&gt; — the sync side of the same story: configuring the psycopg3 pool, sizing &lt;code&gt;min_size&lt;/code&gt;/&lt;code&gt;max_size&lt;/code&gt;, and avoiding the &lt;code&gt;CONN_MAX_AGE&lt;/code&gt; foot-gun.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="footnotes" role="doc-endnotes"&gt;&#10;&lt;hr&gt;&#10;&lt;ol&gt;&#10;&lt;li id="fn:1"&gt;&#10;&lt;p&gt;&lt;a href="https://peps.python.org/pep-0249/" rel="noopener external"&gt;PEP 249 — Python Database API Specification v2.0&lt;/a&gt; — The DB-API2 spec that Django&amp;rsquo;s ORM is built on top of; asyncpg deliberately does not implement it.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:2"&gt;&#10;&lt;p&gt;&lt;a href="https://www.psycopg.org/psycopg3/docs/advanced/async.html" rel="noopener external"&gt;psycopg3 — Async operations&lt;/a&gt; — Official psycopg documentation on the async connection and cursor API.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:3"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/6.0/releases/6.0/" rel="noopener external"&gt;Django 6.0 Release Notes&lt;/a&gt; — Full changelog including async ORM changes, background tasks, and template partials.&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>Django Modern REST</title><link>https://saurabh-kumar.com/til/1775131200/</link><pubDate>Thu, 02 Apr 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1775131200/</guid><description>&lt;p&gt;&lt;a href="https://django-modern-rest.readthedocs.io" rel="noopener external"&gt;Django Modern REST&lt;/a&gt; seems to be an emerging REST API library with focus on performance, scalability and pluggable serializers. Supporting msgpack, CBV and less of sync_to_async. Might evaluate it against the my current preferred &lt;code&gt;django-ninja&lt;/code&gt;. &lt;a href="https://gemini.google.com/app/1aa113e0b21dd18f" rel="noopener external"&gt;gemini&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>zoocache</title><link>https://saurabh-kumar.com/til/1770984000/</link><pubDate>Fri, 13 Feb 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1770984000/</guid><description>&lt;p&gt;&lt;a href="https://github.com/albertobadia/zoocache" rel="noopener external"&gt;zoocache&lt;/a&gt; is a sematic dependency based cache manager, that support in-memory, LMDB or redis backends. Integration with &lt;a href="https://github.com/albertobadia/zoocache/blob/main/docs/django_user_guide.md" rel="noopener external"&gt;Django&lt;/a&gt; looks interesting.&lt;/p&gt;&#10;</description></item><item><title>Free-threaded Python in uv and GitHub Actions</title><link>https://saurabh-kumar.com/til/1768219200/</link><pubDate>Mon, 12 Jan 2026 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1768219200/</guid><description>&lt;p&gt;The free-threaded version of python can be added to uv and github actions with &lt;code&gt;t&lt;/code&gt; suffix. e.g. &lt;code&gt;3.14t&lt;/code&gt; and &lt;code&gt;3.13t&lt;/code&gt; &lt;a href="https://github.com/actions/setup-python?tab=readme-ov-file#basic-usage" rel="noopener external"&gt;source&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>how uv got so fast</title><link>https://saurabh-kumar.com/til/1766836800/</link><pubDate>Sat, 27 Dec 2025 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1766836800/</guid><description>&lt;p&gt;&lt;a href="https://nesbitt.io/2025/12/26/how-uv-got-so-fast.html" rel="noopener external"&gt;how uv got so fast&lt;/a&gt;. UV performance is because of design decisions while rust contributes to micro optimizations.&lt;/p&gt;&#10;</description></item><item><title>LangGraph</title><link>https://saurabh-kumar.com/til/1766577600/</link><pubDate>Wed, 24 Dec 2025 12:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/til/1766577600/</guid><description>&lt;p&gt;&amp;ldquo;&lt;a href="https://docs.langchain.com/oss/python/langgraph/overview" rel="noopener external"&gt;LangGraph&lt;/a&gt; is an orchestration framework for building stateful multi-agent applications using LLMs. It provides low-level primitives such as nodes and edges, along with built-in features that give developers granular control over agent workflows, memory management and state persistence. This means developers can start with a simple pre-built graph and scale to complex, evolving agent architectures. With support for streaming, advanced context management and resilience patterns like model fallbacks and tool error handling, LangGraph enables you to build robust, production-grade agentic applications. Its graph-based approach ensures predictable, customizable workflows and simplifies debugging and scaling.&amp;rdquo;&lt;/p&gt;&#10;</description></item><item><title>Django Native PostgreSQL Connection Pooling</title><link>https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/</link><pubDate>Wed, 18 Jun 2025 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2025/06/django-native-postgresql-connection-pooling/</guid><description>&lt;p&gt;Opening PostgreSQL connections adds overhead. Django 5.1 (released August 7, 2024) added native connection pooling&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, which lets an application reuse connections. Measure connection overhead and response times before and after enabling it; the benefit depends on your workload.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;If you&amp;rsquo;re on Django 6+ and running async/ASGI, also see the async pool notes in &lt;a href="https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/"&gt;How Django&amp;rsquo;s ORM Went From&#10;sync_to_async Threads to Native&#10;psycopg3&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 id="native-pooling-vs-external-pooling"&gt;Native Pooling vs External Pooling&lt;/h2&gt;&#10;&lt;p&gt;PgBouncer runs as a separate pooling server. Django’s native pooling runs in the application process, so it does not require a separate server. It does require psycopg’s pool package. Third-party Django pooling packages and manual connection management are other options, with their own maintenance and configuration needs.&lt;/p&gt;&#10;&lt;h2 id="configure-native-pooling"&gt;Configure Native Pooling&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;: Django 5.1+ and PostgreSQL (this feature does not work with&#10;psycopg2)&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&#10;&lt;p&gt;Install the correct package:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pip install &lt;span class="s2"&gt;&amp;#34;psycopg[binary,pool]&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Critical&lt;/strong&gt;: The package is called &lt;code&gt;psycopg&lt;/code&gt; (not &lt;code&gt;psycopg3&lt;/code&gt;). The pool&#10;functionality requires a separate package that this command installs&#10;automatically&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;Add these lines to your &lt;code&gt;settings.py&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_user&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_password&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_host&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PORT&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;5432&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Required - pooling fails without this&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Deploy this change. Your app now reuses connections instead of creating new ones&#10;for every request.&lt;/p&gt;&#10;&lt;h2 id="prevent-the-configuration-error-that-breaks-production"&gt;Prevent the Configuration Error That Breaks Production&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Must set &lt;code&gt;CONN_MAX_AGE = 0&lt;/code&gt;&lt;/strong&gt;. Without this, Django throws&#10;&lt;code&gt;ImproperlyConfigured: Pooling doesn't support persistent connections&lt;/code&gt; and your&#10;app crashes&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;. The pool manages connection lifetimes, making Django&amp;rsquo;s&#10;persistence redundant.&lt;/p&gt;&#10;&lt;h2 id="scale-for-high-traffic-production"&gt;Scale for High-Traffic Production&lt;/h2&gt;&#10;&lt;p&gt;Replace &lt;code&gt;&amp;quot;pool&amp;quot;: True&lt;/code&gt; with specific parameters for production loads:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;default&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ENGINE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;django.db.backends.postgresql&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;NAME&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;USER&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_user&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PASSWORD&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_password&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;HOST&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;your_db_host&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;PORT&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;5432&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;CONN_MAX_AGE&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;OPTIONS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;pool&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;min_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Keeps connections warm&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_size&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Handles traffic spikes&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;timeout&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Fails fast under extreme load&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_lifetime&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# 30 minutes maximum connection age&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_idle&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# Close idle connections after 5 minutes&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Start with &lt;code&gt;max_size&lt;/code&gt; equal to expected concurrent users divided by 10. Scale up&#10;if you see timeout errors in logs.&lt;/p&gt;&#10;&lt;h2 id="monitor-your-performance-gains"&gt;Monitor Your Performance Gains&lt;/h2&gt;&#10;&lt;p&gt;Enable pool monitoring to track your improvements:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# In settings.py&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;logging&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;psycopg.pool&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Watch pool utilization, connection wait times, timeout errors, and response times. Record median and 95th percentile latency before and after the change under comparable load. Use those measurements to size the pool and judge whether pooling helps your application.&lt;/p&gt;&#10;&lt;h2 id="critical-fix-for-threading-and-celery-applications"&gt;Critical Fix for Threading and Celery Applications&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;Essential for background processing&lt;/strong&gt;: This isn&amp;rsquo;t a pooling bug — it&amp;rsquo;s expected&#10;Django behavior that pooling simply makes painful. Any connection opened outside&#10;Django&amp;rsquo;s request-response cycle (for example, in a thread you spawn yourself)&#10;stays open until you explicitly close it. Without a pool you might not notice;&#10;with one, those un-returned connections exhaust &lt;code&gt;max_size&lt;/code&gt; and you start seeing&#10;&lt;code&gt;PoolTimeout&lt;/code&gt; errors&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;. Add explicit cleanup:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;threading&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Thread&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataProcessingThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;# Your database operations here&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_seen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;# Critical: returns the connection to the pool&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For code that may touch multiple database aliases, Django&amp;rsquo;s documented helper&#10;&lt;code&gt;django.db.close_old_connections()&lt;/code&gt; is the more robust choice — it closes all&#10;old or unusable connections across every configured database:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;close_old_connections&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataProcessingThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_seen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;close_old_connections&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without this cleanup, each background thread permanently holds a connection from&#10;your pool.&lt;/p&gt;&#10;&lt;h2 id="deployment-scenarios-that-require-different-approaches"&gt;Deployment Scenarios That Require Different Approaches&lt;/h2&gt;&#10;&lt;p&gt;&lt;strong&gt;ASGI applications&lt;/strong&gt;: On Django 5.1–5.2, Django&amp;rsquo;s documentation recommends&#10;against native pooling with ASGI — use PgBouncer or similar external poolers&#10;instead. This changed in Django 6.0, which ships an async-aware&#10;&lt;code&gt;AsyncConnectionPool&lt;/code&gt; that works correctly under ASGI. I cover how that fits&#10;together with the truly-async ORM in &lt;a href="https://saurabh-kumar.com/articles/2026/06/django-orm-from-sync_to_async-threads-to-native-psycopg3/"&gt;How Django&amp;rsquo;s ORM Went From sync_to_async&#10;Threads to Native psycopg3&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Serverless deployments&lt;/strong&gt;: Connection pools don&amp;rsquo;t persist across serverless&#10;invocations. Skip this optimization for Lambda/Cloud Functions.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Multi-tenant applications&lt;/strong&gt;: Each tenant database needs its own pool&#10;configuration in &lt;code&gt;DATABASES&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="when-to-evaluate-pooling"&gt;When to Evaluate Pooling&lt;/h2&gt;&#10;&lt;p&gt;Evaluate pooling when connection establishment is a measurable part of request latency, concurrency is growing, or the application is approaching PostgreSQL connection limits. Compare measurements before and after rather than assuming a fixed latency or cost saving.&lt;/p&gt;&#10;&lt;div class="footnotes" role="doc-endnotes"&gt;&#10;&lt;hr&gt;&#10;&lt;ol&gt;&#10;&lt;li id="fn:1"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/5.1/releases/5.1/" rel="noopener external"&gt;Django 5.1 Release Notes&lt;/a&gt; - Official Django documentation announcing native connection pooling support&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:2"&gt;&#10;&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/5.2/ref/databases/" rel="noopener external"&gt;Django Database Configuration&lt;/a&gt; - Complete guide to Django database settings including pooling requirements&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:3"&gt;&#10;&lt;p&gt;&lt;a href="https://www.psycopg.org/psycopg3/docs/basic/install.html" rel="noopener external"&gt;psycopg Installation Guide&lt;/a&gt; - Official psycopg documentation for package installation and pool dependencies&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:4"&gt;&#10;&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/78879329/django-improperlyconfigured-pooling-doesnt-support-persistent-connections" rel="noopener external"&gt;Stack Overflow: Django Pooling Configuration Error&lt;/a&gt; - Community discussion of the CONN_MAX_AGE requirement&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li id="fn:5"&gt;&#10;&lt;p&gt;&lt;a href="https://code.djangoproject.com/ticket/35672" rel="noopener external"&gt;Django Ticket #35672&lt;/a&gt; - Closed as &lt;em&gt;invalid&lt;/em&gt;: connections opened in threads must be returned manually. See also the &lt;a href="https://docs.djangoproject.com/en/stable/ref/databases/#caveats" rel="noopener external"&gt;Databases → Caveats&lt;/a&gt; docs, which recommend &lt;code&gt;django.db.close_old_connections()&lt;/code&gt; for connections created outside the request-response cycle.&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>ty: Astral's Take on Python Type Checking</title><link>https://saurabh-kumar.com/articles/2025/05/ty-astrals-take-on-python-type-checking/</link><pubDate>Fri, 09 May 2025 00:00:00 +0000</pubDate><guid>https://saurabh-kumar.com/articles/2025/05/ty-astrals-take-on-python-type-checking/</guid><description>&lt;p&gt;Anyone who has run &lt;code&gt;mypy&lt;/code&gt; on a real codebase knows the workarounds. You enable the daemon, you keep the incremental cache warm, you scope the check to the files you touched, and you still wait a few seconds for the editor to catch up after a rename. In CI it is worse, because the cache is cold and the full run can stretch into the minutes. None of this is broken, exactly. It is just the cost you learn to pay. So when a new checker says the wait can mostly go away, I pay attention, but I also want to know what it costs.&lt;/p&gt;&#10;&lt;p&gt;That checker is &lt;code&gt;ty&lt;/code&gt; (&lt;a href="https://github.com/astral-sh/ty" rel="noopener external"&gt;gh:astral-sh/ty&lt;/a&gt;), from Astral, the same people who built the Ruff linter and the &lt;code&gt;uv&lt;/code&gt; package manager. As of May 2025, it is pre-alpha, version &lt;code&gt;0.0.0a6&lt;/code&gt;, and not something you should put in production. But it is already worth talking about, partly for what it does and partly for what it deliberately leaves out.&lt;/p&gt;&#10;&lt;h2 id="speed-is-the-headline"&gt;Speed Is The Headline&lt;/h2&gt;&#10;&lt;p&gt;The numbers people are reporting are large. One user saw mypy take 18 seconds where &lt;code&gt;ty&lt;/code&gt; finished the same check in about half a second. Another compared &lt;code&gt;ty&lt;/code&gt; at 2.5 seconds against Pyright at 13.6 seconds on a 100k line codebase. Some users assumed it had failed, because nothing that finishes that fast could have actually looked at their whole project.&lt;/p&gt;&#10;&lt;p&gt;Ruff did the same thing to linting a few years ago. It was fast enough that the linter stopped being a separate step you ran and became something that just happened while you wrote code. &lt;code&gt;ty&lt;/code&gt; looks like it wants to do that for type checking. That matters more than it sounds. A check that takes a minute is a thing you run in CI and forget about. A check that runs in milliseconds is a thing your editor can run on every save, and that changes how it feels to write Python at all.&lt;/p&gt;&#10;&lt;h2 id="fast-and-frequently-wrong-for-now"&gt;Fast And Frequently Wrong, For Now&lt;/h2&gt;&#10;&lt;p&gt;Speed is the easy part. Being correct is the hard part, and here &lt;code&gt;ty&lt;/code&gt; is still young.&lt;/p&gt;&#10;&lt;p&gt;The Astral developers say this plainly: it is pre-alpha, many of the errors it reports may be wrong, and the feature set is small. Right now it does diagnostics and go-to-type-definition, and not much else. One user ran it on a codebase where Pyright reported 10 diagnostics, all correct, and &lt;code&gt;ty&lt;/code&gt; reported 1,599. That is not a small gap. Type inference is the thing that takes years to get right, and &lt;code&gt;ty&lt;/code&gt; has not had years.&lt;/p&gt;&#10;&lt;p&gt;I would not read those 1,599 diagnostics as a verdict on the project. A count like that is rarely 1,599 distinct bugs; it is usually a handful of inference patterns the checker gets wrong, multiplied across every file that hits them. Fix the pattern and the count collapses. What you are looking at is a checker that is fast first and accurate later. Given what Astral did with Ruff and uv, betting against the accuracy catching up seems unwise. But it has not caught up yet, and you should treat the output accordingly.&lt;/p&gt;&#10;&lt;h2 id="it-is-entering-a-crowded-field"&gt;It Is Entering A Crowded Field&lt;/h2&gt;&#10;&lt;p&gt;&lt;code&gt;ty&lt;/code&gt; is not arriving to an empty room. It joins mypy, Microsoft&amp;rsquo;s Pyright and Pylance, and Facebook&amp;rsquo;s Pyrefly, which also builds on Ruff&amp;rsquo;s parser despite being a separate project.&lt;/p&gt;&#10;&lt;p&gt;What makes all of these hard is Python itself. TypeScript was built with static typing in mind. Python&amp;rsquo;s type hints arrived later and grew unevenly, bolted onto a dynamic language, with the inconsistencies you would expect from something added after the fact. On top of that, the libraries people actually use are the hardest cases. Django and SQLAlchemy lean heavily on metaprogramming and runtime code generation, which is exactly the kind of thing a static checker cannot see. The ecosystem mostly papered over this with type stubs and plugins: SQLAlchemy 2.0 added real typed mappings, and Django users lean on &lt;code&gt;django-stubs&lt;/code&gt;, which is a mypy plugin. Hold onto that detail, because it runs straight into the next decision.&lt;/p&gt;&#10;&lt;h2 id="no-plugins-on-purpose"&gt;No Plugins, On Purpose&lt;/h2&gt;&#10;&lt;p&gt;This is where Astral makes a real choice rather than a faster one. mypy lets libraries ship plugins so the checker can understand their metaprogramming. &lt;code&gt;ty&lt;/code&gt; will not have plugins. Astral treats the interchangeability of type checking as a feature: a check should mean the same thing across tools and projects, and it should not depend on a plugin that one checker has and another does not. Instead they plan to support the popular libraries directly in the tool.&lt;/p&gt;&#10;&lt;p&gt;That is a trade, not a free win. The Django user relying on &lt;code&gt;django-stubs&lt;/code&gt; today is relying on exactly the plugin mechanism &lt;code&gt;ty&lt;/code&gt; is choosing not to have, so until Astral supports those libraries directly, that user has no escape hatch. More work falls on Astral, and the libraries they have not reached yet are simply not understood. But it also means you do not end up with type checking that only works inside one vendor&amp;rsquo;s setup, and that division is worth defending.&lt;/p&gt;&#10;&lt;p&gt;They are also borrowing from Rust&amp;rsquo;s compiler for their diagnostics, aiming for error messages that explain rather than just flag. An LSP server and a VS Code plugin are planned, though there is no release date for either.&lt;/p&gt;&#10;&lt;h2 id="the-question-that-always-comes-back"&gt;The Question That Always Comes Back&lt;/h2&gt;&#10;&lt;p&gt;Astral is a VC-backed company, and the same question hangs over &lt;code&gt;ty&lt;/code&gt; as over Ruff and uv: how does this get paid for. The code is open source and has outside contributors, but at some point the money has to come from somewhere.&lt;/p&gt;&#10;&lt;p&gt;People guess at CI products, private repositories, or enterprise services. Astral, for now, seems to be building the tools first and leaving monetization for later. That has earned them a lot of goodwill in the Python community, and it should also keep you a little cautious, because the terms you adopt a tool under are not always the terms you keep it under.&lt;/p&gt;&#10;&lt;h2 id="trying-it"&gt;Trying It&lt;/h2&gt;&#10;&lt;p&gt;If you want to look at it yourself:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;uv tool install ty&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or without installing anything:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;uvx ty check&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;These notes describe the pre-alpha release in May 2025. Expect rough edges, and do not trust a red diagnostic until you have checked it by hand.&lt;/p&gt;&#10;&lt;p&gt;I do not know yet whether &lt;code&gt;ty&lt;/code&gt; becomes the type checker people reach for. The speed is real, the missing plugin system is a deliberate stance and not an oversight, and the accuracy is not there yet. What I want from it is the thing Ruff already delivered: a tool fast enough to disappear into the edit loop, honest enough to trust, and available under open source terms I can rely on. The first of those is here. The other two are still being written.&lt;/p&gt;&#10;</description></item></channel></rss>