Created
May 20, 2021 20:38
-
-
Save victusfate/baaa5288cfdb32d8617d84c605c74713 to your computer and use it in GitHub Desktop.
thread safe multiprocess python dictionary example https://stackoverflow.com/a/46228938/51700
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process, Manager | |
manager = Manager() | |
d = manager.dict() | |
def f(): | |
d[1].append(4) | |
# the __str__() method of a dict object invokes __repr__() on each of its items, | |
# so explicitly invoking __str__() is required in order to print the actual list items | |
print({k: str(v) for k, v in d.items()} | |
if __name__ == '__main__': | |
d[1] = manager.list() | |
p = Process(target=f) | |
p.start() | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment