from sqlalchemy import create_engine, event from sqlalchemy.orm import sessionmaker, declarative_base DATABASE_URL = "sqlite:///./scrapper.db" engine = create_engine( DATABASE_URL, # timeout: how long a connection waits for the SQLite file lock before # raising "database is locked" — needed because several worker threads # write concurrently through their own sessions. connect_args={"check_same_thread": False, "timeout": 30}, pool_pre_ping=True, ) @event.listens_for(engine, "connect") def _set_sqlite_pragma(dbapi_connection, connection_record): """Enable WAL so readers don't block writers, and give writers a generous busy_timeout. Applied to every new connection in the pool.""" cursor = dbapi_connection.cursor() cursor.execute("PRAGMA journal_mode=WAL") cursor.execute("PRAGMA busy_timeout=30000") cursor.execute("PRAGMA synchronous=NORMAL") cursor.close() SessionLocal = sessionmaker( autocommit=False, autoflush=False, bind=engine, ) Base = declarative_base()