diff --git a/src/snek/system/mapper.py b/src/snek/system/mapper.py index bda128a..e399152 100644 --- a/src/snek/system/mapper.py +++ b/src/snek/system/mapper.py @@ -10,10 +10,11 @@ class BaseMapper: model_class: BaseModel = None default_limit: int = DEFAULT_LIMIT table_name: str = None - + semaphore = asyncio.Semaphore(1) + def __init__(self, app): self.app = app - self.semaphore = asyncio.Semaphore(1) + self.default_limit = self.__class__.default_limit @property @@ -26,19 +27,16 @@ class BaseMapper: async def run_in_executor(self, func, *args, **kwargs): use_semaphore = kwargs.pop("use_semaphore", False) - if use_semaphore: - async with self.semaphore: - database_exception = None - for x in range(20): - try: - result = func(*args, **kwargs) - self.db.commit() - return result - except Exception as ex: - await asyncio.sleep(0) - database_exception = ex - raise database_exception - return await self.loop.run_in_executor(None, lambda: func(*args, **kwargs)) + + def _execute(): + result = func(*args, **kwargs) + if use_semaphore: + self.db.commit() + return result + + async with self.semaphore: + return await self.loop.run_in_executor(None, _execute) + async def new(self): return self.model_class(mapper=self, app=self.app) diff --git a/src/snek/templates/web.html b/src/snek/templates/web.html index fc32770..81b2abb 100644 --- a/src/snek/templates/web.html +++ b/src/snek/templates/web.html @@ -32,7 +32,7 @@ {% endfor %}
-