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>You need to lock the variable like this:</p>

<pre><code>from threading import Lock

lock = Lock()
set_this_var_only_once = None

def worker():
    with lock:
        if set_this_var_only_once is None:
            set_this_var_only_once = "not None
</code></pre>

python multithreading python-2.7