Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Does the same apply to dictionaries? Or is a dictionary a collection of variables?</p> </blockquote> <p>Let's be more general:</p> <p><strong>What does "atomic operation" mean?</strong></p> <p>From <a href="http://en.wikipedia.org/wiki/Atomic_operation">Wikipedia</a> :</p> <blockquote> <p>In concurrent programming, an operation (or set of operations) is atomic, linearizable, indivisible or uninterruptible if it appears to the rest of the system to occur instantaneously. Atomicity is a guarantee of isolation from concurrent processes.</p> </blockquote> <p><strong>Now what does this mean in Python?</strong></p> <p>This means that each bytecode instruction is atomic (at least for Python &lt;3.2, before the new GIL).</p> <p><strong>Why is that???</strong></p> <p>Because Python (CPython) use a <a href="http://en.wikipedia.org/wiki/Global_Interpreter_Lock">Global Interpreter Lock (GIL)</a>. The CPython interpreter uses a lock to make sure that only one thread runs in the interpreter at a time, and uses a "check interval" (see <a href="http://docs.python.org/library/sys.html#sys.getcheckinterval"><code>sys.getcheckinterval()</code></a>) to know how many bytecode instructions to execute before switching between threads (by default set to 100).</p> <p><strong>So now what does this mean??</strong></p> <p>It means that operations that can be represented by only one bytecode instruction are atomic. For example, incrementing a variable is <em>not</em> atomic, because the operation is done in three bytecode instructions:</p> <pre><code>&gt;&gt;&gt; import dis &gt;&gt;&gt; def f(a): a += 1 &gt;&gt;&gt; dis.dis(f) 2 0 LOAD_FAST 0 (a) 3 LOAD_CONST 1 (1) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Operation 1 Load 6 INPLACE_ADD &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Operation 2 iadd 7 STORE_FAST 0 (a) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Operation 3 store 10 LOAD_CONST 0 (None) 13 RETURN_VALUE </code></pre> <p><strong>So what about dictionaries??</strong></p> <p>Some operations are atomic; for example, this operation is atomic:</p> <pre><code>d[x] = y d.update(d2) d.keys() </code></pre> <p>See for yourself:</p> <pre><code>&gt;&gt;&gt; def f(d): x = 1 y = 1 d[x] = y &gt;&gt;&gt; dis.dis(f) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 1 (x) 3 6 LOAD_CONST 1 (1) 9 STORE_FAST 2 (y) 4 12 LOAD_FAST 2 (y) 15 LOAD_FAST 0 (d) 18 LOAD_FAST 1 (x) 21 STORE_SUBSCR &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; One operation 22 LOAD_CONST 0 (None) 25 RETURN_VALUE </code></pre> <p>See <a href="http://docs.python.org/library/dis.html#opcode-STORE_SUBSCR">this</a> to understand what STORE_SUBSCR does.</p> <p>But as you see, it is not totally true, because this operation:</p> <pre><code> ... 4 12 LOAD_FAST 2 (y) 15 LOAD_FAST 0 (d) 18 LOAD_FAST 1 (x) ... </code></pre> <p>can make the entire operation not atomic. Why? Let's say the variable x can also be changed by another thread...or that you want another thread to clear your dictionary...we can name many cases when it can go wrong, so it is complicated! And so here we will apply <a href="http://en.wikipedia.org/wiki/Murphy_law">Murphy's Law</a>: "Anything that can go wrong, will go wrong".</p> <p><strong>So what now?</strong></p> <p>If you still want to share variables between thread, use a lock:</p> <pre><code>import threading mylock = threading.RLock() def atomic_operation(): with mylock: print "operation are now atomic" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload