patx/pickledb
added aiofiles use for asave()
Commit 443f0c2 · patx · 2025-02-08T21:08:57-05:00
Comments
No comments yet.
Diff
diff --git a/pickledb.py b/pickledb.py
index 658215b..5040531 100644
--- a/pickledb.py
+++ b/pickledb.py
@@ -31,6 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import asyncio
import os
+import aiofiles
import orjson
@@ -264,5 +265,13 @@ class AsyncPickleDB(PickleDB):
Returns:
bool: True if save was successful, False if not.
"""
+ temp_location = f"{self.location}.tmp"
async with self._lock:
- return self.save()
+ try:
+ async with aiofiles.open(temp_location, "wb") as temp_file:
+ await temp_file.write(orjson.dumps(self.db, option=option))
+ os.replace(temp_location, self.location)
+ return True
+ except Exception as e:
+ print(f"Failed to save database: {e}")
+ return False