곧장 부딪힐 flag
기본값으로 Python sqlite3.Connection 이 만든 thread 에서만 사용 가능. 다른 thread 에서 쓰면 ProgrammingError: SQLite objects created in a thread can only be used in that same thread.
Multi-threaded 앱 처리 3 방법:
- Thread 마다 connection — 보통
threading.local(). 단순 + 안전. - Connection pool — 짧은-수명 thread 많은 앱. SQLAlchemy 같은 도구가 알아서.
- check_same_thread=False + 본인 lock — 안전 체크 끄고 직접 access serialize. 망치기 쉬움; 정확히 뭐 하는지 알 때만.
Self-reference: 피파 backend 가 thread 대신
aiosqlite (track 7) 사용 — 그게 이걸 통째로 우회, async task 가 정의상 한 thread. Sync threaded 코드는 per-thread connection 패턴이 boring + correct 기본.