perf: 쿠팡·배송·경비·말레이시아·휴가 라우터 async def → def
동기 psycopg/파일 I/O 를 async def 안에서 부르면 쿼리·엑셀 파싱 동안 이벤트 루프가 멈춰 앱 전체(다른 모듈 포함) 요청이 줄줄이 대기했다. sync def 로 두면 FastAPI 가 스레드풀에서 돌린다. - await 없는 핸들러 116개는 async 키워드만 제거(AST 로 기계 변환). - 업로드: await up.read() → up.file.read() (쿠팡 매출/발주 업로드, 경비 첨부 청크 읽기). - 말레이시아 폼 핸들러 6개: request.form() 은 async 라 _request_form 의존성(인증 뒤에 실행)으로 미리 받아 넘기고 본문은 def. - Google Sheets writer: httplib2 가 스레드 안전하지 않고 같은 날짜 시트를 동시에 쓰면 섞이므로 RLock 으로 직렬화. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,8 +22,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
|
||||
@@ -40,6 +42,20 @@ def _hex_color(value: str) -> dict[str, float]:
|
||||
}
|
||||
|
||||
|
||||
# 웹 핸들러가 sync def(스레드풀)라 여러 요청이 동시에 이 writer 를 쓸 수 있다.
|
||||
# googleapiclient 의 HTTP 클라이언트(httplib2)는 스레드 안전하지 않고, 같은 날짜
|
||||
# 시트를 동시에 지우고/쓰면 내용이 섞이므로 시트 작업은 한 번에 하나씩만 한다.
|
||||
_LOCK = threading.RLock()
|
||||
|
||||
|
||||
def _serialized(fn: Any) -> Any:
|
||||
@functools.wraps(fn)
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
with _LOCK:
|
||||
return fn(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
class GoogleSheetsWriter:
|
||||
"""시트 1장을 통째로 다시 쓰는 용도의 얇은 래퍼."""
|
||||
|
||||
@@ -153,6 +169,7 @@ class GoogleSheetsWriter:
|
||||
).execute()
|
||||
return int(res["replies"][0]["addSheet"]["properties"]["sheetId"])
|
||||
|
||||
@_serialized
|
||||
def delete_sheet(self, *, spreadsheet_id: str, title: str) -> dict[str, Any]:
|
||||
"""이름이 정확히 일치하는 시트 1장을 삭제한다.
|
||||
|
||||
@@ -190,6 +207,7 @@ class GoogleSheetsWriter:
|
||||
).execute()
|
||||
return {"ok": True, "action": "deleted", "title": title}
|
||||
|
||||
@_serialized
|
||||
def write_table(
|
||||
self,
|
||||
*,
|
||||
@@ -346,6 +364,7 @@ _writer: GoogleSheetsWriter | None = None
|
||||
def get_writer() -> GoogleSheetsWriter:
|
||||
"""프로세스 1개당 1회 초기화."""
|
||||
global _writer # noqa: PLW0603
|
||||
if _writer is None:
|
||||
_writer = GoogleSheetsWriter()
|
||||
with _LOCK:
|
||||
if _writer is None:
|
||||
_writer = GoogleSheetsWriter()
|
||||
return _writer
|
||||
|
||||
Reference in New Issue
Block a user