Skip to content

Instantly share code, notes, and snippets.

@victusfate
Created May 20, 2021 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/baaa5288cfdb32d8617d84c605c74713 to your computer and use it in GitHub Desktop.
Save victusfate/baaa5288cfdb32d8617d84c605c74713 to your computer and use it in GitHub Desktop.
thread safe multiprocess python dictionary example https://stackoverflow.com/a/46228938/51700
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