Absolutely not.
It is quite possible that two threads execute this line before executing the next one:
if set_this_var_only_once is None:
After that, both threads will execute the next line:
set_this_var_only_once = "not None"
You can use locks to prevent that:
lock = threading.Lock()
def worker():
lock.acquire()
if set_this_var_only_once is None:
set_this_var_only_once = "not None"
lock.release()
Only one thread will be able to acquire the lock. If another thread tries to acquire it while it is locked, the call to lock.acquire() will block and wait until the lock is released by the first thread. Then the lock will be acquired by the other thread.
That way it is ensured that the code between lock.acquire() and lock.release() is executed in one thread at a time.
EDIT
As Gerhard pointed in the other answer, you can use the context management protocol with locks:
with lock:
if set_this_var_only_once is None:
set_this_var_only_once = "not None"
That will also make sure the lock is correctly released in case of an exception within the locked block.