Spaces:
Running
Running
Update app/main.py
Browse files- app/main.py +12 -2
app/main.py
CHANGED
|
@@ -7,8 +7,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 7 |
from pydantic import BaseModel, HttpUrl, root_validator, ValidationError
|
| 8 |
from fastapi.encoders import jsonable_encoder
|
| 9 |
|
| 10 |
-
from app.lens_images_core import translate_lens
|
| 11 |
-
# from app.lens_text_core import translate_lens_text
|
| 12 |
|
| 13 |
PORT = int(os.getenv("PORT", 8080))
|
| 14 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 8))
|
|
@@ -231,6 +231,15 @@ async def cleanup():
|
|
| 231 |
for jid in [k for k,v in results.items() if v.get("_created_at") < cutoff]:
|
| 232 |
results.pop(jid, None)
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
@app.on_event("startup")
|
| 235 |
async def startup():
|
| 236 |
if ENABLE_BACKGROUND_WORKERS:
|
|
@@ -239,6 +248,7 @@ async def startup():
|
|
| 239 |
# for _ in range(MAX_WORKERS_TEXT):
|
| 240 |
# asyncio.create_task(worker("lens_text", jobq_text))
|
| 241 |
asyncio.create_task(cleanup())
|
|
|
|
| 242 |
log.info(
|
| 243 |
"startup OK – %d image workers, %d text workers, TTL=%ds (workers_enabled=%s)",
|
| 244 |
MAX_WORKERS_IMAGES, MAX_WORKERS_TEXT, RESULTS_TTL, ENABLE_BACKGROUND_WORKERS
|
|
|
|
| 7 |
from pydantic import BaseModel, HttpUrl, root_validator, ValidationError
|
| 8 |
from fastapi.encoders import jsonable_encoder
|
| 9 |
|
| 10 |
+
from app.lens_images_core import translate_lens, _cookie_header as img_cookie_header
|
| 11 |
+
# from app.lens_text_core import translate_lens_text, prewarm_driver as text_prewarm
|
| 12 |
|
| 13 |
PORT = int(os.getenv("PORT", 8080))
|
| 14 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 8))
|
|
|
|
| 231 |
for jid in [k for k,v in results.items() if v.get("_created_at") < cutoff]:
|
| 232 |
results.pop(jid, None)
|
| 233 |
|
| 234 |
+
async def keep_warm_loop():
|
| 235 |
+
while True:
|
| 236 |
+
try:
|
| 237 |
+
await img_cookie_header()
|
| 238 |
+
# await text_prewarm()
|
| 239 |
+
except Exception as e:
|
| 240 |
+
log.warning("keep_warm skipped: %s", e)
|
| 241 |
+
await asyncio.sleep(600)
|
| 242 |
+
|
| 243 |
@app.on_event("startup")
|
| 244 |
async def startup():
|
| 245 |
if ENABLE_BACKGROUND_WORKERS:
|
|
|
|
| 248 |
# for _ in range(MAX_WORKERS_TEXT):
|
| 249 |
# asyncio.create_task(worker("lens_text", jobq_text))
|
| 250 |
asyncio.create_task(cleanup())
|
| 251 |
+
asyncio.create_task(keep_warm_loop())
|
| 252 |
log.info(
|
| 253 |
"startup OK – %d image workers, %d text workers, TTL=%ds (workers_enabled=%s)",
|
| 254 |
MAX_WORKERS_IMAGES, MAX_WORKERS_TEXT, RESULTS_TTL, ENABLE_BACKGROUND_WORKERS
|