I'm wondering if dict.update()
is Python thread safe. I've read the related questions, but none of them exactly addresses my question.
My question is very specific and simple. For example, I already have a local dictionary d2
. I simply need to update the global dictionary d
with d2
as shown below. d
starts out empty and fills up with different threads. The d2
in each thread may have overlapping entries with d
(don't think this matters). Is it thread safe?
import dis
def f(d):
d2 = {1:2, 3:4}
d.update(d2)
print(dis.dis(f))
The bytecode looks like the following:
10 0 LOAD_CONST 1 (2)
2 LOAD_CONST 2 (4)
4 LOAD_CONST 3 ((1, 3))
6 BUILD_CONST_KEY_MAP 2
8 STORE_FAST 1 (d2)
11 10 LOAD_FAST 0 (d)
12 LOAD_ATTR 0 (update)
14 LOAD_FAST 1 (d2)
16 CALL_FUNCTION 1
18 POP_TOP
20 LOAD_CONST 0 (None)
22 RETURN_VALUE
It looks like 16 CALL_FUNCTION
is the atomic function that updates the dictionary. So it should be thread safe?