multithreading - thread safe python dictionaries? -
i have function call starts 10 threads. before start of these threads , have
from collections import defaultdict output = defaultdict(dict)
and output empty.
each thread generate data write dictionary.
something like:
output['water'] = 'h20' output['fire'] = 'delta of oxygen' ....
the threads add items , not iterate on of other items or modify other items. output['water']
being item different output['fire']
. can guarantee no 2 threads going create same item. is, each thread t has unique i. in code: output[i] unique per thread.
is dictionary thread safe in regard?
yes.
if using cpython , strings keys, yes. gil in cpython ensures 1 thread executes bytecode @ time, , setting key value in dict happens in single opcode, store_subscr
. if not using cpython, or using key has custom __hash__
, __eq__
, or __cmp__
methods, bets off. if had soapbox, i'd hop on , warn of evils of relying on implementation details correctness. it's more pythonic of write works case , in environment used, since doing otherwise seen premature optimization. enjoy working code!
>>> dis import dis >>> dis(compile('output = defaultdict(dict); output["water"] = "h2o"', 'example', 'exec')) 1 0 load_name 0 (defaultdict) 3 load_name 1 (dict) 6 call_function 1 (1 positional, 0 keyword pair) 9 store_name 2 (output) 12 load_const 0 ('h2o') 15 load_name 2 (output) 18 load_const 1 ('water') 21 store_subscr 22 load_const 2 (none) 25 return_value
this has been discussed elsewhere.
Comments
Post a Comment