Since this thing is on the innerwebs I suppose it should have a
[python_utils.git] / collect / shared_dict.py
index ac390bc600e769a94c2205440883257a665917bb..3207927ed2f550b6516bce0c1b72fd96d7581ba4 100644 (file)
@@ -4,7 +4,7 @@
 The MIT License (MIT)
 
 Copyright (c) 2020 LuizaLabs
-Additions Copyright (c) 2022 Scott Gasch
+Additions/Modifications Copyright (c) 2022 Scott Gasch
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -29,7 +29,6 @@ This class is based on https://github.com/luizalabs/shared-memory-dict
 
 import pickle
 from contextlib import contextmanager
-from functools import wraps
 from multiprocessing import RLock, shared_memory
 from typing import (
     Any,
@@ -42,8 +41,6 @@ from typing import (
     ValuesView,
 )
 
-from decorator_utils import synchronized
-
 
 class PickleSerializer:
     def dumps(self, obj: dict) -> bytes:
@@ -94,9 +91,7 @@ class SharedDict(object):
             return shared_memory.SharedMemory(name=name, create=True, size=size_bytes)
 
     def _ensure_memory_initialization(self):
-        memory_is_empty = (
-            bytes(self.shared_memory.buf).split(SharedDict.NULL_BYTE, 1)[0] == b''
-        )
+        memory_is_empty = bytes(self.shared_memory.buf).split(SharedDict.NULL_BYTE, 1)[0] == b''
         if memory_is_empty:
             self.clear()
 
@@ -108,7 +103,8 @@ class SharedDict(object):
     def cleanup(self) -> None:
         if not hasattr(self, 'shared_memory'):
             return
-        self.shared_memory.unlink()
+        with SharedDict.MPLOCK:
+            self.shared_memory.unlink()
 
     def clear(self) -> None:
         self._save_memory({})