sinful1992 commited on
Commit
7ae5e53
·
1 Parent(s): b010bd2

fix(ocr): run warm-up in background so server accepts connections immediately

Browse files
Files changed (1) hide show
  1. main.py +17 -10
main.py CHANGED
@@ -9,6 +9,7 @@ Usage:
9
  uvicorn main:app --host 0.0.0.0 --port 8000 --reload
10
  """
11
 
 
12
  import logging
13
  from contextlib import asynccontextmanager
14
 
@@ -26,20 +27,26 @@ logger = logging.getLogger(__name__)
26
  _reader: PaddleOCRReader | None = None
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  @asynccontextmanager
30
  async def lifespan(app: FastAPI):
31
  global _reader
32
  _reader = PaddleOCRReader()
33
- # Warm up PaddlePaddle JIT compilation so the first real request is fast.
34
- # Without this, the first inference takes 60-90s (JIT compile on CPU),
35
- # which exceeds the app's 45s timeout even though the server is "ready".
36
- import numpy as np
37
- logger.info("Warming up OCR inference engine...")
38
- try:
39
- _reader.extract(np.ones((200, 600, 3), dtype=np.uint8) * 255)
40
- logger.info("OCR warm-up complete.")
41
- except Exception as exc:
42
- logger.warning("OCR warm-up failed (non-fatal): %s", exc)
43
  yield
44
  _reader = None
45
 
 
9
  uvicorn main:app --host 0.0.0.0 --port 8000 --reload
10
  """
11
 
12
+ import asyncio
13
  import logging
14
  from contextlib import asynccontextmanager
15
 
 
27
  _reader: PaddleOCRReader | None = None
28
 
29
 
30
+ async def _background_warmup():
31
+ import numpy as np
32
+ logger.info("Background warm-up starting...")
33
+ try:
34
+ await asyncio.get_event_loop().run_in_executor(
35
+ None, lambda: _reader.extract(np.ones((200, 600, 3), dtype=np.uint8) * 255)
36
+ )
37
+ logger.info("Background warm-up complete.")
38
+ except Exception as exc:
39
+ logger.warning("Background warm-up failed (non-fatal): %s", exc)
40
+
41
+
42
  @asynccontextmanager
43
  async def lifespan(app: FastAPI):
44
  global _reader
45
  _reader = PaddleOCRReader()
46
+ # Run warm-up in background so the server starts accepting connections
47
+ # immediately. First real request after restart may still be slow (JIT),
48
+ # but the server won't appear unresponsive during startup.
49
+ asyncio.create_task(_background_warmup())
 
 
 
 
 
 
50
  yield
51
  _reader = None
52