<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Piergiuseppe D&apos;Abbraccio</title><description>Articles by Piergiuseppe D&apos;Abbraccio on Python, SQLite and backend engineering.</description><link>https://www.piergiuseppedabbraccio.com/</link><language>en</language><item><title>What broke when I turned the GIL off</title><link>https://www.piergiuseppedabbraccio.com/en/articles/what-broke-when-i-turned-the-gil-off/</link><guid isPermaLink="true">https://www.piergiuseppedabbraccio.com/en/articles/what-broke-when-i-turned-the-gil-off/</guid><description>Four races that a passing test suite never showed, one I added myself while removing a lock, and the import that turned the GIL back on with a single warning.</description><pubDate>Tue, 01 Dec 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For years, Python code with threads has leaned on a rule nobody wrote down, that only one thread runs at a time. A check and the write that follows it are two steps, but another thread almost never got in between. Free-threaded Python takes that away.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/Piergiuseppe/tinyjobs&quot;&gt;tinyjobs&lt;/a&gt; is a job queue I maintain. I made its worker run several event loops in threads on Python 3.14t. The whole suite, 1,706 tests, was green before the audit began. This post is about what was broken anyway. An audit found four races, I added a fifth myself, and one import put the GIL back with a single warning on stderr.&lt;/p&gt;
&lt;h2 id=&quot;safe-by-accident&quot;&gt;Safe by accident&lt;/h2&gt;
&lt;p&gt;The bug has one shape, and you have written it.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; key &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; cache:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;    cache[key] &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; build(key)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; cache[key]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each line is safe on its own. The three together are not, because another thread can change &lt;code&gt;cache&lt;/code&gt; between any two of them.&lt;/p&gt;
&lt;p&gt;The GIL never made this correct. It made it rare. A thread keeps the interpreter for five milliseconds before it has to give it up, and these lines take a few microseconds. Without the GIL, two threads run them at the same moment on two cores. A window that was open one time in a thousand is now open every time.&lt;/p&gt;
&lt;h2 id=&quot;two-threads-one-name&quot;&gt;Two threads, one name&lt;/h2&gt;
&lt;p&gt;tinyjobs keeps a registry of tasks by name. A task that isn’t there yet can be resolved on first use, which is how Django’s tasks are found.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(self, name):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._tasks.get(name) &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._resolve(name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt; _resolve&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(self, name):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; resolver &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._resolvers:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;        task &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; resolver(name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; task &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;.register(task)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; task&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thread A and thread B both ask for the same name, and both find nothing. Both call the resolver, and each gets its own task object, wrapping its own new function. A registers first. B arrives, finds the name bound to a different function, and &lt;code&gt;register&lt;/code&gt; does what it was built to do with two functions under one name, and raises &lt;code&gt;TaskAlreadyRegisteredError&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I ran the real Django backend with six request threads enqueueing the same task for the first time. On 3.14t, 129 of 1,195 enqueues failed. With &lt;code&gt;PYTHON_GIL=1&lt;/code&gt;, on the same build and the same script, none did.&lt;/p&gt;
&lt;p&gt;On the worker side it was worse, though I had to build the case. Django’s worker command discovers tasks before it starts, so this only reaches custom resolvers and tasks outside &lt;code&gt;&amp;lt;app&amp;gt;.tasks&lt;/code&gt;. I took a worker with four loops, a resolver slowed by two milliseconds to stand in for a first import, and 192 jobs due at the same instant. The error isn’t one the worker expects from a lookup, so a job that hit it stayed &lt;code&gt;running&lt;/code&gt; until its lease expired. That happened to 20 jobs in one run and 28 in the other.&lt;/p&gt;
&lt;p&gt;The fix is one line.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._tasks.setdefault(task.name, task)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;setdefault&lt;/code&gt; checks and writes under the dictionary’s own lock. One thread wins, and the other gets the winner’s task.&lt;/p&gt;
&lt;h2 id=&quot;a-cache-that-was-empty-for-a-moment&quot;&gt;A cache that was empty for a moment&lt;/h2&gt;
&lt;p&gt;The JSON codec keeps a cache from type to adapter. Registering a new type rebuilds it.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt; _rebuild_type_cache&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(self):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;    self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._by_type &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; adapter &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; reversed&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._adapters):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; adapter_type &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; adapter.types:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._by_type[adapter_type] &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; adapter&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first line publishes an empty dictionary, and the loop fills it while everyone can see it. A reader does the usual two steps, &lt;code&gt;not in&lt;/code&gt; and then &lt;code&gt;[...]&lt;/code&gt;, and they can now land on two different dictionaries. Three threads encoding a &lt;code&gt;datetime&lt;/code&gt; while a fourth registered an enum got 45 &lt;code&gt;KeyError&lt;/code&gt;s without the GIL, and none with it.&lt;/p&gt;
&lt;p&gt;The fix has two halves. The writer builds the new dictionary in a local variable and assigns it at the end, so nobody sees it half full. The reader takes &lt;code&gt;self._by_type&lt;/code&gt; once and works on that one object.&lt;/p&gt;
&lt;h2 id=&quot;a-flag-nobody-heard&quot;&gt;A flag nobody heard&lt;/h2&gt;
&lt;p&gt;The other two are not the GIL’s fault. They come from the threads themselves. The moment a worker has several loops, things get called from the wrong one.&lt;/p&gt;
&lt;p&gt;A worker stops when a flag is raised. The flag wraps an asyncio future, and &lt;code&gt;set()&lt;/code&gt; resolved it directly.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(self):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;    self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._set &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._future &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;is&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._future.done():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;._future.set_result(&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A future belongs to one event loop. Resolving it from another thread changes its state, but the loop is asleep in &lt;code&gt;select&lt;/code&gt; and nobody wakes it. The stop request is there. The loop just doesn’t look.&lt;/p&gt;
&lt;p&gt;A review of the same branch had already measured what this costs, on a worker pointed at an address that never answers, with a fifteen-second connect timeout, and asked to stop after one second. It stopped after 15.0. On main the same stop took 1.1 seconds, so this was my own doing. The branch had moved the connect onto the worker’s own loop, which sleeps until the socket answers. I closed that path.&lt;/p&gt;
&lt;p&gt;The audit found the same mistake still reachable from a loop that had just started. Forty starts with a stop aimed at that moment showed nothing, so this one was fixed without being caught in the act. Now &lt;code&gt;set()&lt;/code&gt; checks whose future it is, and hands the result to the owner with &lt;code&gt;loop.call_soon_threadsafe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The fourth race was also found by reading the code, and I never saw it happen. Each loop cleared a shutdown deadline on the shared app as it started, with no lock, so a loop that started late could erase a deadline another had just set. The window is a few instructions wide. I fixed it anyway, and only the first loop clears it now.&lt;/p&gt;
&lt;h2 id=&quot;the-one-i-added-myself&quot;&gt;The one I added myself&lt;/h2&gt;
&lt;p&gt;With the GIL off, a profile showed the thread pool for &lt;code&gt;def&lt;/code&gt; tasks taking its lock three times per job. I replaced the count of idle threads with a semaphore, as the standard library’s pool does. A thread that finishes a job releases a permit, and a submit that gets a permit knows a thread is waiting. Handing a job to a thread went from 63 to 32 microseconds.&lt;/p&gt;
&lt;p&gt;A review found the hole. A thread finishes job A, and before it releases its permit, job B is submitted. No permit is free and the pool is at its size, so B just sits in the queue. The thread releases its permit, loops, and takes B itself. Now there is one permit and zero idle threads.&lt;/p&gt;
&lt;p&gt;On its own that costs microseconds. It becomes a hang in the one pool that replaces stuck threads. When a job outlives its timeout, that pool is allowed one more thread, but it started the thread only if a job was already waiting. So B runs past its timeout, the queue is empty, and no thread starts.&lt;/p&gt;
&lt;p&gt;Job C arrives, takes the stale permit, and trusts it. Nobody is coming. C waits for a thread that is already busy, while the pool was allowed a new one. If every thread is stuck like B’s, C never starts, and its own timeout can’t save it, because the timeout starts when the job does.&lt;/p&gt;
&lt;p&gt;The old code, with its lock, could not do this. The fix keeps the fast path and takes the lock again only at the two places where the pool changes size. A second review found another way to reach the same state, through a thread that fails to start. The third found nothing.&lt;/p&gt;
&lt;h2 id=&quot;the-gil-that-came-back&quot;&gt;The GIL that came back&lt;/h2&gt;
&lt;p&gt;One problem was not a race at all. &lt;code&gt;redis-py&lt;/code&gt; imports &lt;code&gt;hiredis&lt;/code&gt; when it is installed, to parse replies faster. &lt;code&gt;hiredis&lt;/code&gt; ships wheels for 3.14t, but it doesn’t declare itself safe without the GIL, so the interpreter turns the GIL back on when it loads.&lt;/p&gt;
&lt;p&gt;The interpreter says so once, in a &lt;code&gt;RuntimeWarning&lt;/code&gt; on stderr at import time, which is easy to lose in a worker’s log. After that nothing fails. The worker runs, the tests pass, and every benchmark measures the wrong thing. So the worker now checks at startup.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;sysconfig.get_config_var(&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt;&quot;Py_GIL_DISABLED&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; sys._is_gil_enabled()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A free-threaded build with the GIL enabled gets a line in the log, and CI has a test that imports every tinyjobs module and checks the GIL is still off.&lt;/p&gt;
&lt;h2 id=&quot;does-it-hold&quot;&gt;Does it hold?&lt;/h2&gt;
&lt;p&gt;A fix for a race is easy to believe and hard to prove, so each one got a test, and each test had to fail on the old code first. Two of mine didn’t. The cache test passed on the old code, and the flag test hung there, where it should have failed. Both were rewritten until the old code gave the right error, which is &lt;code&gt;TaskAlreadyRegisteredError&lt;/code&gt; for the registry, &lt;code&gt;KeyError&lt;/code&gt; for the cache and, for the flag, the &lt;code&gt;Non-thread-safe operation&lt;/code&gt; that asyncio raises in debug mode.&lt;/p&gt;
&lt;p&gt;The tests say the fixes hold. Whether the threads are worth having is a separate question, so I also ran the whole thing under load, on a ten-core laptop with two Redis instances.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1 process, 8 loops, Python 3.14t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;42,000 async def jobs/s, 97% of what 8 processes do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;with the GIL on, the same threads flatten at 27,000&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;3.14t now runs in CI next to 3.12, 3.13 and 3.14. The full numbers are on the &lt;a href=&quot;https://tinyjobs.readthedocs.io/en/latest/measured/&quot;&gt;Measured&lt;/a&gt; page.&lt;/p&gt;
&lt;h2 id=&quot;what-i-cant-promise&quot;&gt;What I can’t promise&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;An audit finds what it looks for.&lt;/strong&gt; Two of the four races were reproduced. The other two were found by reading the code and never seen in a run. I have no way to count the ones nobody thought of.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Your tasks are in the same position.&lt;/strong&gt; &lt;code&gt;def&lt;/code&gt; tasks in one worker now run truly in parallel. If two of them share a dictionary, a counter or a client, they have the bugs above.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Your dependencies decide too.&lt;/strong&gt; One C extension that hasn’t declared itself safe turns the GIL back on for the whole process.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;thats-the-whole-trick&quot;&gt;That’s the whole trick&lt;/h2&gt;
&lt;p&gt;Read once, write once, and build new state in private before you publish it. Almost every fix here was one of those three, and most were under five lines. Finding them was the work.&lt;/p&gt;
&lt;p&gt;tinyjobs is on &lt;a href=&quot;https://github.com/Piergiuseppe/tinyjobs&quot;&gt;GitHub&lt;/a&gt; and &lt;a href=&quot;https://pypi.org/project/tinyjobs/&quot;&gt;PyPI&lt;/a&gt;, and the &lt;a href=&quot;https://tinyjobs.readthedocs.io/en/latest/free-threaded-python/&quot;&gt;free-threaded Python&lt;/a&gt; page has the setup.&lt;/p&gt;
&lt;p&gt;If you have run your own code on a free-threaded build, I’d like to hear what broke first.&lt;/p&gt;
</content:encoded></item><item><title>How a job queue fits in one SQLite file</title><link>https://www.piergiuseppedabbraccio.com/en/articles/how-a-job-queue-fits-in-one-sqlite-file/</link><guid isPermaLink="true">https://www.piergiuseppedabbraccio.com/en/articles/how-a-job-queue-fits-in-one-sqlite-file/</guid><description>What stops two workers from running the same job, what happens when one dies in the middle, and where SQLite stops being enough.</description><pubDate>Wed, 18 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Most Python projects I have worked on needed background jobs long before they needed a message broker. A welcome email, a PDF to render, a nightly report. Each time the answer was the same: add Redis, add Celery, add a worker service, then monitor all three.&lt;/p&gt;
&lt;p&gt;A few months ago I got tired of it and started &lt;a href=&quot;https://github.com/Piergiuseppe/tinyjobs&quot;&gt;tinyjobs&lt;/a&gt;, a job queue where the queue is a SQLite file. This post is about how that works: what stops two workers from running the same job, what happens when a worker dies in the middle of one, and where SQLite stops being enough.&lt;/p&gt;
&lt;h2 id=&quot;a-job-is-a-row&quot;&gt;A job is a row&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; tinyjobs &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; TinyJobs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; TinyJobs(&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt;&quot;sqlite:///jobs.db&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt;@app.task&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E36209&quot;&gt;max_attempts&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt; send_welcome_email&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;(user_id: &lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;) -&amp;gt; &lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;send_welcome_email.enqueue(&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;enqueue&lt;/code&gt; inserts one row into a table called &lt;code&gt;tinyjobs_jobs&lt;/code&gt;: the task name, the arguments as JSON, the status &lt;code&gt;queued&lt;/code&gt;, and a &lt;code&gt;run_at&lt;/code&gt; time. That row is the whole message. A worker is a process that looks for rows it can run.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;run_at&lt;/code&gt; column does more work than it seems. A delayed job is a row with &lt;code&gt;run_at&lt;/code&gt; in the future. A retry is the same row with &lt;code&gt;run_at&lt;/code&gt; pushed forward by the backoff. A cron schedule inserts a row at each due time. Three features, one column, and one question for the worker: which rows are due?&lt;/p&gt;
&lt;h2 id=&quot;claiming-one-statement-one-winner&quot;&gt;Claiming: one statement, one winner&lt;/h2&gt;
&lt;p&gt;The dangerous moment in any queue is the claim. If two workers both see a job as free and both take it, it runs twice. The classic bug is reading in one statement and writing in the next, because another worker can slip in between.&lt;/p&gt;
&lt;p&gt;tinyjobs finds and takes the jobs in a single statement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; tinyjobs_jobs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;   SET&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; &apos;running&apos;&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;       worker_id &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ?,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;       attempts &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; attempts &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;       claims &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; claims &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;       lease_expires_at &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; WHERE&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;       SELECT&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; tinyjobs_jobs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;        WHERE&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; &apos;queued&apos;&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; run_at &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ? &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; queue&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; (?)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;        ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; priority DESC&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;, run_at &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;ASC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;        LIMIT&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt; )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;RETURNING &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It runs inside a &lt;code&gt;BEGIN IMMEDIATE&lt;/code&gt; transaction, which takes SQLite’s write lock up front. SQLite allows one writer at a time, and that is usually listed as its weakness. Here it is the feature: the lock puts the claims in a line. A second worker waits its turn (up to a busy timeout, five seconds by default), and when it gets the lock its subquery no longer sees the rows the first one took.&lt;/p&gt;
&lt;p&gt;The file is in WAL mode, so readers never wait for the writer. Your application, a dashboard or a &lt;code&gt;sqlite3&lt;/code&gt; shell can read the queue while workers claim from it.&lt;/p&gt;
&lt;h2 id=&quot;when-a-worker-dies&quot;&gt;When a worker dies&lt;/h2&gt;
&lt;p&gt;The claim also writes &lt;code&gt;lease_expires_at&lt;/code&gt;, sixty seconds ahead. The lease is a promise with an expiry date: “this job is mine until then”. While the job runs, the worker renews the lease every twenty seconds.&lt;/p&gt;
&lt;p&gt;A dead worker stops renewing. Every worker also runs a small loop, the reaper, every thirty seconds. It looks for rows that are &lt;code&gt;running&lt;/code&gt; with a lease in the past, records that attempt as &lt;code&gt;lost&lt;/code&gt;, and sets the job back to &lt;code&gt;queued&lt;/code&gt;. There is no coordinator and no leader: any worker that is alive does it, and the write lock makes sure only one of them wins each row.&lt;/p&gt;
&lt;p&gt;A graceful stop is kinder. On &lt;code&gt;SIGTERM&lt;/code&gt; the worker stops claiming, gives running jobs time to finish, and hands the ones it can interrupt back to the queue before it exits, so they don’t wait for a lease.&lt;/p&gt;
&lt;p&gt;Either way, a job that was interrupted starts again from the beginning. The function’s progress lived in the memory of a process that is gone, and the row only says which function to call and with which arguments. So the guarantee is at-least-once: a job is never lost, but it can run twice, and tasks should be safe to repeat.&lt;/p&gt;
&lt;h2 id=&quot;the-answer-that-arrives-late&quot;&gt;The answer that arrives late&lt;/h2&gt;
&lt;p&gt;There is a subtler case. Worker A claims a job and then freezes for two minutes: a paused VM, a laptop lid, a very long garbage collection. Its lease expires, the reaper gives the job to worker B, and then A wakes up and tries to record “success”.&lt;/p&gt;
&lt;p&gt;Every write of an outcome checks who holds the job:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; tinyjobs_jobs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;   SET&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; &apos;success&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; WHERE&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ? &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; &apos;running&apos;&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; worker_id &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ? &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; claims &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; ?;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;claims&lt;/code&gt; is a counter that goes up on every claim, so an old attempt can’t be mistaken for the current one, even on the same worker. A’s write matches zero rows and changes nothing. The job belongs to B now.&lt;/p&gt;
&lt;h2 id=&quot;does-it-hold&quot;&gt;Does it hold?&lt;/h2&gt;
&lt;p&gt;I didn’t want to trust the reasoning alone, so I ran it for ten hours on a small shared VPS: one SQLite file, three worker processes, 25 jobs per second, a graceful restart every fifteen minutes and a &lt;code&gt;SIGKILL&lt;/code&gt; every twenty.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;897,300 jobs, 24.9/s sustained, zero enqueue failures&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;68 worker restarts, 29 of them by SIGKILL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;nothing lost&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Worker memory went from 37.1 MB in the first hour to 37.3 MB in the last, and the p95 from enqueue to finish stayed around 400 ms. For raw throughput, four worker processes drain about 5,200 no-op jobs per second from one file on a local SSD. The limit is &lt;code&gt;fsync&lt;/code&gt;, not Python.&lt;/p&gt;
&lt;p&gt;You don’t have to take my word for it: &lt;code&gt;tinyjobs verify&lt;/code&gt; reruns the experiment on your own machine, kills included. The full numbers are on the &lt;a href=&quot;https://tinyjobs.readthedocs.io/en/latest/measured/&quot;&gt;Measured&lt;/a&gt; page.&lt;/p&gt;
&lt;h2 id=&quot;two-things-a-broker-cant-give-you&quot;&gt;Two things a broker can’t give you&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The queue is a database you can read.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1&quot;&gt;sqlite3&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; jobs.db&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt; &quot;SELECT task, status, attempts FROM tinyjobs_jobs WHERE status = &apos;failed&apos;;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;A job can commit with your own data.&lt;/strong&gt; If the queue lives in the file your application already uses, a job can be enqueued in the same transaction as your rows:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; connection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;    connection.execute(&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt;&quot;INSERT INTO orders (id) VALUES (?)&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;,))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;    send_receipt.with_options(&lt;/span&gt;&lt;span style=&quot;color:#E36209&quot;&gt;tx&lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;connection).enqueue(&lt;/span&gt;&lt;span style=&quot;color:#005CC5&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the transaction rolls back, the job never existed. If it commits, the job is there. No order without a receipt, and no receipt for an order that failed.&lt;/p&gt;
&lt;h2 id=&quot;where-sqlite-stops&quot;&gt;Where SQLite stops&lt;/h2&gt;
&lt;p&gt;It would be dishonest to stop here, so:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;One machine.&lt;/strong&gt; The file has to be on a local disk. SQLite’s locking over NFS or SMB is unreliable, so workers on several servers can’t share it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One writer.&lt;/strong&gt; Thousands of jobs per second is the ceiling, not the starting point.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Workers poll.&lt;/strong&gt; SQLite can’t wake a process up, so an idle worker checks the file, backing off to twice a second.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you hit one of these, the same tasks run on Postgres or Redis by changing the URL:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-light&quot; style=&quot;background-color:#fff;color:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#D73A49&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt; TinyJobs(&lt;/span&gt;&lt;span style=&quot;color:#032F62&quot;&gt;&quot;postgresql://app@localhost/shop&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On Postgres the claim uses &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; and workers are woken with &lt;code&gt;LISTEN/NOTIFY&lt;/code&gt; instead of polling. The &lt;code&gt;tinyjobs move&lt;/code&gt; command carries the jobs still to run from one store to the other.&lt;/p&gt;
&lt;h2 id=&quot;thats-the-whole-trick&quot;&gt;That’s the whole trick&lt;/h2&gt;
&lt;p&gt;A table, one atomic statement, a lease, and a check before every write. Most of the work went into the cases around them: shutdowns, signals, clocks and crashes at the worst possible moment.&lt;/p&gt;
&lt;p&gt;tinyjobs is on &lt;a href=&quot;https://github.com/Piergiuseppe/tinyjobs&quot;&gt;GitHub&lt;/a&gt; and &lt;a href=&quot;https://pypi.org/project/tinyjobs/&quot;&gt;PyPI&lt;/a&gt;, with no dependencies on SQLite. It can run &lt;a href=&quot;https://tinyjobs.readthedocs.io/en/latest/fastapi/&quot;&gt;inside a FastAPI process&lt;/a&gt; and works as a &lt;a href=&quot;https://tinyjobs.readthedocs.io/en/latest/django/&quot;&gt;&lt;code&gt;django.tasks&lt;/code&gt; backend&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you have run a queue on SQLite, or decided not to, I’d like to hear what you ran into.&lt;/p&gt;
</content:encoded></item></channel></rss>