Python: multithreading setting the variable once
<p>Does the following code thread-safe?<br>
Will only one/first thread set the variable, set_this_var_only_once?  </p>

<pre><code>set_this_var_only_once = None

def worker():
    global set_this_var_only_once
    if set_this_var_only_once is None:
        set_this_var_only_once = "not None"

for i in range(10):
    t = threading.Thread( target=worker )
    t.daemon=True
    t.start()
</code></pre>

<p>No it's not. </p>

<blockquote>
  <p>If another thread gets control after the current thread has fetched
  the variable, it may fetch the variable, increment it, and write it
  back, before the current thread does the same thing. And since theyâre
  both seeing the same original value, only one item will be accounted
  for.</p>
</blockquote>

<p><a href="http://effbot.org/zone/thread-synchronization.htm" rel="nofollow">Here's</a> this good article about it. </p>

<p>P.S. Also the following line is needed in the worker(): </p>

<pre><code>global set_this_var_only_once 
</code></pre>

python multithreading python-2.7