CodexBloom - Programming Q&A Platform

Trouble with threading and SQLite in Python 3.9 causing database locked scenarios

๐Ÿ‘€ Views: 40 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-12
python flask sqlite threading Python

This might be a silly question, but I'm stuck on something that should probably be simple... I'm working with a `sqlite3.OperationalError: database is locked` behavior when using threading to handle database operations in Python 3.9. I'm using the `sqlite3` library to interact with a local SQLite database. I have a simple web server built using Flask that launches a new thread to handle incoming requests and perform database operations. Hereโ€™s a simplified version of my code: ```python import sqlite3 import threading from flask import Flask, request app = Flask(__name__) def db_query(query): conn = sqlite3.connect('mydatabase.db') cursor = conn.cursor() cursor.execute(query) conn.commit() cursor.close() conn.close() @app.route('/update', methods=['POST']) def update_database(): data = request.json query = f"UPDATE my_table SET value = '{data['value']}' WHERE id = {data['id']}" thread = threading.Thread(target=db_query, args=(query,)) thread.start() return 'Update initiated', 202 if __name__ == '__main__': app.run(debug=True) ``` The scenario arises when multiple requests are made simultaneously to the `/update` endpoint. When this happens, I frequently get the `database is locked` behavior. Iโ€™ve tried using a connection pool, but that didnโ€™t help. I've also looked into using `sqlite3.connect('mydatabase.db', check_same_thread=False)` to allow multiple threads to use the same connection, but this doesn't seem to resolve the scenario either. Iโ€™m aware that SQLite has limitations when it comes to concurrent writes. Is there a recommended pattern to follow when working with Flask, SQLite, and threading that might help in avoiding this behavior? Or should I consider using a different approach or database? Any insights would be greatly appreciated! What's the best practice here?