<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Soumik</title>
    <description>The latest articles on DEV Community by Soumik (@soumik15630m).</description>
    <link>https://dev.to/soumik15630m</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4044863%2Fc0edbfce-ca52-4eb5-9674-5de1bb7f4c0f.png</url>
      <title>DEV Community: Soumik</title>
      <link>https://dev.to/soumik15630m</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/soumik15630m"/>
    <language>en</language>
    <item>
      <title>I parallelize Python loops with two comments - and guarantee identical results published: true</title>
      <dc:creator>Soumik</dc:creator>
      <pubDate>Fri, 24 Jul 2026 06:24:18 +0000</pubDate>
      <link>https://dev.to/soumik15630m/i-parallelize-python-loops-with-two-comments-and-guarantee-identical-resultspublished-true-4b4m</link>
      <guid>https://dev.to/soumik15630m/i-parallelize-python-loops-with-two-comments-and-guarantee-identical-resultspublished-true-4b4m</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs0352ve1iv1ti50fpqu1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs0352ve1iv1ti50fpqu1.gif" alt="Lucen parallelizing a marked loop with bit-identical output" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python has your cores. It just doesn't use them. A CPU-bound &lt;code&gt;for&lt;/code&gt; loop runs on one core while the rest sit idle, and the usual fixes (&lt;code&gt;multiprocessing&lt;/code&gt;, &lt;code&gt;concurrent.futures&lt;/code&gt;, &lt;code&gt;joblib&lt;/code&gt;) ask you to restructure code, manage pools and pickling, and reason about what's actually safe to run in parallel. That's real work, and it's easy to get subtly wrong.&lt;/p&gt;

&lt;p&gt;I wanted something else: mark a loop, and if it's safe and worth it, run it in parallel; otherwise run it exactly as before. No rewrite. No wrong answers. That's &lt;strong&gt;Lucen&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole API is two comments
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# LUCEN START
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;expensive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# LUCEN END
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the file and that loop runs across your cores:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;lucen
lucen run yourscript.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delete the two comments and the file is ordinary Python again, byte for byte.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one guarantee
&lt;/h2&gt;

&lt;p&gt;Most "fast Python" tools ask you to trust them. Lucen makes exactly one promise, with no tiers and no opt-out:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A parallel run is bit-identical to the same file run as plain sequential Python, floating-point results and container insertion order included.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not "close." Not "for the mode you enabled." Identical. Floats reduce in sequential order so the last bit matches; lists and dicts keep the exact order they'd have single-threaded. If Lucen can't guarantee that for a loop, it doesn't parallelize it; it runs sequentially and tells you why.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it decides
&lt;/h2&gt;

&lt;p&gt;Two questions, both answered before anything runs in parallel:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Is it safe?&lt;/strong&gt; Lucen analyzes the loop body (reads, writes, aliasing, and the shape of any cross-iteration dependency) and only parallelizes when it can prove iterations don't interfere in a way it can't reconcile. When it can't read a called function's source, you vouch for it with &lt;code&gt;# LUCEN TRUST&lt;/code&gt;; otherwise it stays conservative.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Is it worth it?&lt;/strong&gt; Parallelism costs dispatch, and on some interpreters, pickling. Lucen probes the real per-iteration cost and only parallelizes when the speedup beats that overhead. A trivial loop stays sequential, because making it "parallel" would make it slower.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  It picks the backend for you
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvobyw6eg5jnyb7bdt3yy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvobyw6eg5jnyb7bdt3yy.png" alt=" " width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The right way to run parallel Python depends on the interpreter, not your code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On a normal (GIL) build, CPU-bound Python can't speed up with threads, so Lucen routes to &lt;strong&gt;processes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;On free-threaded (no-GIL) CPython 3.13/3.14, threads finally parallelize Python, so Lucen routes to &lt;strong&gt;threads&lt;/strong&gt;, skipping pickling and subprocess cost entirely.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same two comments either way. On my machine, CPU-bound loops land around &lt;strong&gt;3 to 4.6x&lt;/strong&gt;: via processes on 3.11 and via threads on 3.14t, with no code change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I trust it enough to call it 1.0
&lt;/h2&gt;

&lt;p&gt;Parallelism bugs are the worst bugs: nondeterministic, rare, and disqualifying for a tool whose entire pitch is "identical results." So correctness is the project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Differential testing:&lt;/strong&gt; generated programs run parallel vs. sequential, compared bit-for-bit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Property testing&lt;/strong&gt; with Hypothesis, plus whole-program and front-end fuzzing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TLA+ specifications&lt;/strong&gt; for the privatize-and-commit and wavefront protocols, model-checked.&lt;/li&gt;
&lt;li&gt;An optional Rust core accelerates orchestration, with a pure-Python fallback guaranteed to produce identical results, so &lt;code&gt;pip install&lt;/code&gt; always works and the answer never depends on whether the native core loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apache-2.0, published with trusted publishing and signed releases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;lucen
lucen run yourscript.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mark a slow loop, run it, then delete the comments and confirm the output is identical. That is the whole experience, and the point.&lt;/p&gt;

&lt;p&gt;Repo, benchmarks, and design docs: &lt;a href="https://github.com/fcmv/lucen" rel="noopener noreferrer"&gt;https://github.com/fcmv/lucen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'd love feedback, especially from anyone running CPU-bound Python in anger. What would you point it at?&lt;/p&gt;

</description>
      <category>python</category>
      <category>opensource</category>
      <category>performance</category>
      <category>rust</category>
    </item>
  </channel>
</rss>
