I have seen 2 different answers on thread safety of python function attributes. Assuming a single process with possible multiple threads, and thinking that functions are global, is there a definite problem in using a function attribute as static storage? No answers based on programming style preferences, please.
Asked
Active
Viewed 440 times
0
-
1Why store a global on a function when you could store it in a module? Storing attributes on a function wouldn't be thread safe at all though. It'd be just like storing attributes on any other object that has a `__dict__`. – Dunes Feb 27 '15 at 17:49
-
1What do you mean by static storage? If its set once before the threads are created and only read after that, then yes, its okay. – tdelaney Feb 27 '15 at 18:07
1 Answers
1
There's nothing really wrong with what you describe. A "static" global function object to hold your variables:
from threading import Thread
def static():
pass
static.x = 0
def thread():
static.x += 1
print(static.x)
for nothing in range(10):
Thread(target=thread).start()
Output:
1
2
3
4
5
6
7
8
9
10
That "works" because each thread executes and finishes quickly, and within the same amount of time. But let's say you have threads running for arbitrary lengths of time:
from threading import Thread
from time import sleep
from random import random
from threading import Thread
def static():
pass
static.x = 0
def thread():
static.x += 1
x = static.x
sleep(random())
print(x)
for nothing in range(10):
Thread(target=thread).start()
Output:
2
3
8
1
5
7
10
4
6
9
The behavior becomes undefined.
Amendment:
As tdelaney
pointed out,
"[The] first example only works by luck and would fail randomly on repeated runs..."
The first example is meant to be an illustration of how multithreading can appear to be functioning properly. It is by no means thread-safe code.
Amendment II: You may want to take a look at this question.
-
2Just to keep clear about mutlithreading bugs, your first example only works by luck and would fail randomly on repeated runs. Its not okay to ignore threading issues just because the operation is fast so fails less often. – tdelaney Feb 27 '15 at 18:06
-
-
Thank you for your answers. It appears that my question was naïve. My current view is that function attributes are not thread-safe. I will ask a better question after more reading. The context is avoiding doing repeated long calculations for the same user who is interacting with a webpage in a scenario where each user gets a thread in a single process. Framework is web2py. – jarrodwilcox Feb 28 '15 at 22:25