Blocking 호출이 cooperative scheduling 죽임
Python asyncio 가 기본값으로 single thread 위 모든 거 실행. 한 task 가 I/O 대기 (DB read, HTTP 요청) 면 event loop 가 다른 task 실행. 그게 전체 전제.
함정: asyncio 는 await 할 때만 wait 봐. Disk 에서 200ms block 하는 sync 호출이 200ms 동안 event loop 통째로 freeze — 다른 모든 request, 다른 모든 task, 다른 모든 websocket frame.
평범한 sqlite3.connect(...) + conn.execute(...) 가 sync C 호출. Async 웹 앱 안에서 모든 DB query 가 event loop 멈춤. 유저 한 명이면 상관 X. 동시 100 명이면 무너져.
해법 둘:
- aiosqlite — SQLite 를 백그라운드 thread 에서 실행 + async
execute/fetchone/등 노출. Await 가 DB 호출이 thread 위 도는 동안 event loop 양보. - asyncio.to_thread — sync 호출 수동 offload. 되긴 하는데 aiosqlite 의 connection-management 다시 발명.
Self-reference: 피파 backend (
backend/store/conversations.py) 가 정확히 이 이유로 aiosqlite 사용. Claude SSE streaming 이 conversation 영속화 동안 event loop 양보 계속해야 함; sync write 면 UI 에 보이게 stutter.