Issue36119
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2019-02-26 10:47 by andrei2peu, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Files | ||||
---|---|---|---|---|
File name | Uploaded | Description | Edit | |
image (2).png | andrei2peu, 2019-02-26 10:47 | Behavior example of list |
Messages (3) | |||
---|---|---|---|
msg336642 - (view) | Author: Andrei Stefan (andrei2peu) | Date: 2019-02-26 10:47 | |
I'm creating a shared dict for multiprocessing purposes: from multiprocessing import Manager manager = Manager() shared_dict = manager.dict() If I add a set or a list as a value in the dict: shared_dict['test'] = set() or shared_dict['test'] = list() I can't add/append in that set/list inside the shared dictionary: shared_dict['test'].add(1234) or shared_dict['test'].append(1234) The following expression: print(dict(shared_dict)) Will return: {'test': set()} or {'test': []}. But if I add in the set/list using operators: shared_dict['test'] |= {1234} or shared_dict['test'] += [1234] It will work: {'test': {1234}} or {'test': [1234]}. |
|||
msg336644 - (view) | Author: Karthikeyan Singaravelan (xtreak) * | Date: 2019-02-26 11:01 | |
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]} |
|||
msg336702 - (view) | Author: Josh Rosenberg (josh.r) * | Date: 2019-02-26 17:44 | |
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. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:11 | admin | set | github: 80300 |
2019-02-26 17:44:21 | josh.r | set | status: open -> closed nosy: + josh.r messages: + msg336702 resolution: not a bug stage: resolved |
2019-02-26 11:01:29 | xtreak | set | nosy:
+ pitrou, xtreak messages: + msg336644 |
2019-02-26 10:47:13 | andrei2peu | create |