patx/pickledb
added very basic async support
Commit 24600bf · patx · 2025-02-08T21:02:38-05:00
Comments
No comments yet.
Diff
diff --git a/README.md b/README.md
index 16713fa..c3ed590 100644
--- a/README.md
+++ b/README.md
@@ -443,11 +443,10 @@ For frameworks like FastAPI, Starlette, or MicroPie, use async wrappers to handl
from uuid import uuid4
import asyncio
from MicroPie import App
-from pickledb import PickleDB
from markupsafe import escape
+from pickledb import AsyncPickleDB
-db = PickleDB('pastes.db')
-db_lock = asyncio.Lock()
+db = AsyncPickleDB('pastes.db')
class Root(App):
@@ -455,20 +454,17 @@ class Root(App):
if self.request.method == "POST":
paste_content = self.request.body_params.get('paste_content', [''])[0]
pid = str(uuid4())
- async with db_lock:
- await asyncio.to_thread(db.set, pid, escape(paste_content))
- await asyncio.to_thread(db.save)
+ await db.aset(pid, escape(paste_content))
+ await db.asave()
return self._redirect(f'/paste/{pid}')
return await self._render_template('index.html')
async def paste(self, paste_id, delete=None):
if delete == 'delete':
- async with db_lock:
- await asyncio.to_thread(db.remove, paste_id)
- await asyncio.to_thread(db.save)
+ await db.aremove(paste_id)
+ await db.asave()
return self._redirect('/')
- async with db_lock:
- paste_content = await asyncio.to_thread(db.get, paste_id) or ""
+ paste_content = await db.aget(paste_id)
return await self._render_template(
'paste.html',
paste_id=paste_id,
diff --git a/pickledb.py b/pickledb.py
index e13a88b..658215b 100644
--- a/pickledb.py
+++ b/pickledb.py
@@ -28,6 +28,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
+import asyncio
import os
import orjson
@@ -192,3 +193,76 @@ class PickleDB:
list: A list of all keys.
"""
return list(self.db.keys())
+
+
+class AsyncPickleDB(PickleDB):
+
+ def __init__(self, location):
+ super().__init__(location)
+ self._lock = asyncio.Lock()
+
+ async def aset(self, key, value):
+ """
+ Async version of the set method.
+
+ Args:
+ key (any): The key to set. If the key is not a string, it
+ will be converted to a string.
+ value (any): The value to associate with the key.
+
+ Behavior:
+ - If the key already exists, its value will be updated.
+ - If the key does not exist, it will be added to the
+ database.
+
+ Returns:
+ bool: True if the operation succeeds.
+ """
+ async with self._lock:
+ self.db[str(key)] = value
+ return True
+
+ async def aget(self, key):
+ """
+ Async version of the get method.
+
+ Args:
+ key (any): The key to retrieve. If the key is not a
+ string, it will be converted to a string.
+
+ Returns:
+ any: The value associated with the key, or None if the
+ key does not exist.
+ """
+ async with self._lock:
+ return self.db.get(str(key))
+
+ async def aremove(self, key):
+ """
+ Async version of the remove method.
+
+ Args:
+ key (any): The key to delete. If the key is not a string,
+ it will be converted to a string.
+
+ Returns:
+ bool: True if the key was deleted, False if the key does
+ not exist.
+ """
+ async with self._lock:
+ return self.db.pop(str(key), None) is not None
+
+ async def asave(self):
+ """
+ Async version of the save method.
+
+ Behavior:
+ - Writes to a temporary file and replaces the
+ original file only after the write is successful,
+ ensuring data integrity.
+
+ Returns:
+ bool: True if save was successful, False if not.
+ """
+ async with self._lock:
+ return self.save()
diff --git a/setup.py b/setup.py
index 1525dfa..f54eaad 100644
--- a/setup.py
+++ b/setup.py
@@ -47,14 +47,14 @@ Key Improvements in Version 1.0+
* Streamlined API: Removed legacy methods (e.g., `ladd`, `dmerge`) in favor of native Python operations.
* Unified Handling of Data Types: Treats all Python-native types (lists, dicts, etc.) as first-class citizens.
* Explicit Saves: The `auto_save` feature was removed to provide users greater control and optimize performance.
-* Added fully built in async class for use with even based applications.
+* Added fully built in async class for use with event based applications.
"""
from distutils.core import setup
setup(name="pickleDB",
- version="1.2",
+ version="1.3",
description="A lightweight and simple database using json.",
long_description=__doc__,
author="Harrison Erd",