added aiofiles use for asave()

Commit 443f0c2 · patx · 2025-02-08T21:08:57-05:00

Changeset
443f0c27ef97479e3b803ff1896c8d44222c81e4
Parents
24600bf8aaad0d74e8c6606672a62f7002a69b15

View source at this commit

Comments

No comments yet.

Log in to comment

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