Issue 36119: Can't add/append in set/list inside shared dict (original) (raw)
https://docs.python.org/3/library/multiprocessing.html#proxy-objects
If standard (non-proxy) list or dict objects are contained in a referent, modifications to those mutable values will not be propagated through the manager because the proxy has no way of knowing when the values contained within are modified. However, storing a value in a container proxy (which triggers a setitem on the proxy object) does propagate through the manager and so to effectively modify such an item, one could re-assign the modified value to the container proxy
$ ./python.exe Python 3.8.0a2+ (heads/master:d5a551c269, Feb 26 2019, 15:49:14) [Clang 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information.
from multiprocessing import Manager man = Manager() shared_list = man.dict() shared_list['a'] = list() shared_list['a'].append(100) shared_list <DictProxy object, typeid 'dict' at 0x1096adc50> dict(shared_list) {'a': []} shared_list['a'] += [1000] # Update and assign dict(shared_list) {'a': [1000]} shared_list['a'] += [1000] # Update and assign dict(shared_list) {'a': [1000, 1000]}
As Karthikeyan, this is an inevitable, and documented, consequence of the proxies not being aware of in-place modification of their contents.
As your own example demonstrates, any approach that provides that information to the shared dict proxy will work; |= and += are almost the same as .update and .extend, but implemented such that the left hand side is always reassigned, even when the result of ior/iadd is the same object it was called on. Thus, |=/+= work, while add/append/update/extend do not.
Note that as of 3.6, there is another option: Nested shared objects:
Changed in version 3.6: Shared objects are capable of being nested. For example, a shared container object such as a shared list can contain other shared objects which will all be managed and synchronized by the SyncManager.
So the alternative solution in your case (assuming you're on 3.6 or later, which your bug version tags say you are) is to make the sub-lists manager.lists, or replace use of a set with manager.dict (as dicts with all values set to True, are largely compatible with set anyway, especially with the advent of dict views):
manager = Manager() shared_dict = manager.dict()
shared_dict['test'] = manager.dict() # or shared_dict['test'] = manager.list()
shared_dict['test'][1234] = True # or shared_dict['test'].append(1234)
Downside: The repr of shared dicts/lists doesn't display the contents, so your example code won't make it obvious that the problem is fixed, but it does in fact work. I wrote a terrible JSON one-liner to check the contents, and it demonstrates that the shared dict/list work just fine:
import json from multiprocessing import Manager from multiprocessing.managers import DictProxy, ListProxy
manager = Manager() shared_dict = manager.dict()
shared_dict['testset'] = set() shared_dict['testlist'] = [] shared_dict['testshareddict'] = manager.dict() shared_dict['testsharedlist'] = manager.list()
shared_dict['testset'].add(1234) shared_dict['testlist'].append(1234) shared_dict['testshareddict'][1234] = True shared_dict['testsharedlist'].append(1234)
print(json.dumps(shared_dict, default=lambda x: dict(x) if isinstance(x, DictProxy) else list(x) if isinstance(x, ListProxy) else dict.fromkeys(x, True) if isinstance(x, (set, frozenset)) else x))
The dump shows that the changes to the shared inner dict and list are reflected in the result directly, even with no assignment back to the keys of the outer dict (while, as you note, the plain set and list show no changes).
Closing as not a bug, since this is fully documented, with multiple workarounds available.