Skip to content

Instantly share code, notes, and snippets.

@timothymugayi
Last active April 7, 2022 11:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothymugayi/ff746c099c1c65e2ce0a93067edf7917 to your computer and use it in GitHub Desktop.
Save timothymugayi/ff746c099c1c65e2ce0a93067edf7917 to your computer and use it in GitHub Desktop.
How to update python multiprocessing shared dict
import uuid
import sys
import multiprocessing
lock = multiprocessing.Lock()
if __name__ == '__main__':
print(sys.version);
# Manager().dict() = is a dict that spans multiple processes it exists in shared memory
mpd = multiprocessing.Manager().dict(
{
'tasks': {},
'results': {}
}
);
task_id = str(uuid.uuid4())
tasks = mpd['tasks']
tasks[task_id] = task_id
mpd['tasks'] = tasks
print(mpd)
tasks = mpd['tasks']
tasks.pop(task_id, None)
mpd['tasks'] = tasks
print(mpd)
# when you need to mutual exclusion and you need to guarantee one process updates resources at one time.
with lock:
task_id = str(uuid.uuid4())
tasks = mpd['tasks']
tasks[task_id] = task_id
mpd['tasks'] = tasks
print(mpd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment