X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=collect%2Fshared_dict.py;h=3207927ed2f550b6516bce0c1b72fd96d7581ba4;hb=e46158e49121b8a955bb07b73f5bcf9928b79c90;hp=7c84c14c073743ea1e452d58393f8f29d280ed23;hpb=6ba90a1f30f1c0cf4df12fcd0c62181f29bc3668;p=python_utils.git diff --git a/collect/shared_dict.py b/collect/shared_dict.py index 7c84c14..3207927 100644 --- a/collect/shared_dict.py +++ b/collect/shared_dict.py @@ -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: @@ -74,6 +71,7 @@ class SharedDict(object): super().__init__() self.name = name self._serializer = PickleSerializer() + assert size_bytes is None or size_bytes > 0 self.shared_memory = self._get_or_create_memory_block(name, size_bytes) self._ensure_memory_initialization() self.lock = RLock() @@ -89,13 +87,11 @@ class SharedDict(object): try: return shared_memory.SharedMemory(name=name) except FileNotFoundError: - assert size_bytes + assert size_bytes is not None 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() @@ -107,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({})