Added cache and queue.
All checks were successful
Build Base Application / Build (push) Successful in 2m4s
All checks were successful
Build Base Application / Build (push) Successful in 2m4s
This commit is contained in:
parent
715ba4a41c
commit
626b090d23
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
.vscode
|
||||
.history
|
||||
.backup.*
|
||||
.venv
|
||||
__pycache__
|
||||
.trigger-2024-12-02 13:37:42
|
||||
|
@ -117,6 +117,11 @@ class Agent:
|
||||
print(result)
|
||||
return result
|
||||
|
||||
def upload_file(file_name: str, purpose: str) -> str:
|
||||
with open(file_name, "rb") as file_fd:
|
||||
response = self.client.files.create(file=file_fd, purpose=purpose)
|
||||
return response.id
|
||||
|
||||
async def chat(
|
||||
self, message: str, interval: Optional[float] = 0.2
|
||||
) -> Generator[None, None, str]:
|
||||
|
78
src/app/cache.py
Normal file
78
src/app/cache.py
Normal file
@ -0,0 +1,78 @@
|
||||
# Written by retoor@molodetz.nl
|
||||
|
||||
# This code provides decorators for caching function results with expiration times, supporting both synchronous and asynchronous functions.
|
||||
|
||||
# Imports: `time`, `asyncio`, and `wraps` from `functools` are used in this script.
|
||||
|
||||
# MIT License
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
|
||||
import time
|
||||
import asyncio
|
||||
from functools import wraps
|
||||
|
||||
def time_cache(timeout: int = 600):
|
||||
def decorator(func):
|
||||
cache = {}
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
key = (args, frozenset(kwargs.items()))
|
||||
current_time = time.time()
|
||||
|
||||
if key in cache:
|
||||
result, timestamp = cache[key]
|
||||
if current_time - timestamp < timeout:
|
||||
return result
|
||||
|
||||
result = func(*args, **kwargs)
|
||||
cache[key] = (result, current_time)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
def time_cache_async(timeout: int = 600):
|
||||
def decorator(func):
|
||||
cache = {}
|
||||
|
||||
@wraps(func)
|
||||
async def wrapper(*args, **kwargs):
|
||||
key = (args, frozenset(kwargs.items()))
|
||||
current_time = time.time()
|
||||
|
||||
if key in cache:
|
||||
result, timestamp = cache[key]
|
||||
if current_time - timestamp < timeout:
|
||||
return result
|
||||
|
||||
result = await func(*args, **kwargs)
|
||||
cache[key] = (result, current_time)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
@cache_time_async(timeout=600)
|
||||
async def expensive_async_calculation(x, y):
|
||||
print("Computing...")
|
||||
await asyncio.sleep(2)
|
||||
return x + y
|
44
src/app/queue.py
Normal file
44
src/app/queue.py
Normal file
@ -0,0 +1,44 @@
|
||||
# Written by retoor@molodetz.nl
|
||||
|
||||
# This code defines a CallQueue class that queues function calls and executes them sequentially, supporting both synchronous and asynchronous functions.
|
||||
|
||||
# Imports from standard modules: functools, deque, asyncio
|
||||
|
||||
# MIT License
|
||||
#
|
||||
# (C) 2023 The Author
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# Copies or substantial portions of the Software are reproduced and must include the above copyright notice and other similar permission notice in this particular software, including in any accompanying documentation.
|
||||
|
||||
|
||||
from functools import wraps
|
||||
from collections import deque
|
||||
import asyncio
|
||||
|
||||
class CallQueue:
|
||||
def __init__(self):
|
||||
self.call_queue = deque()
|
||||
|
||||
def __call__(self, func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
self.call_queue.append((func, args, kwargs))
|
||||
return wrapper
|
||||
|
||||
async def execute_next(self):
|
||||
if self.call_queue:
|
||||
func, args, kwargs = self.call_queue.popleft()
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
return await func(*args, **kwargs)
|
||||
else:
|
||||
return func(*args, **kwargs)
|
||||
else:
|
||||
raise IndexError("The call queue is empty.")
|
||||
|
||||
async def execute_all(self):
|
||||
results = []
|
||||
while self.call_queue:
|
||||
results.append(await self.execute_next())
|
||||
return results
|
Loading…
Reference in New Issue
Block a user