Skip to content

Commit d3ca385

Browse files
author
example
committed
added shared db context
1 parent 128aa2f commit d3ca385

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

main.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import tempfile
33
import whisper
44
import sqlite3
5+
from threading import Lock
6+
import os
57
from threading import Thread
68
from datetime import datetime
79
import json
@@ -11,8 +13,19 @@
1113

1214

1315
# Create jobs database if it doesn't exist
16+
def get_db_connection():
17+
conn = sqlite3.connect(
18+
"jobs.db",
19+
check_same_thread=False,
20+
timeout=30, # 30 second timeout
21+
)
22+
conn.row_factory = sqlite3.Row
23+
return conn
24+
25+
1426
def init_db():
15-
conn = sqlite3.connect("jobs.db")
27+
conn = get_db_connection()
28+
c = conn.cursor()
1629
c = conn.cursor()
1730
c.execute("""
1831
CREATE TABLE IF NOT EXISTS jobs (
@@ -36,7 +49,7 @@ def init_db():
3649
def process_job(job_id):
3750
# Create app context for the thread
3851
with app.app_context():
39-
conn = sqlite3.connect("jobs.db")
52+
conn = get_db_connection()
4053
c = conn.cursor()
4154

4255
# Get job details
@@ -153,7 +166,7 @@ def start_transcription():
153166

154167
@app.route("/status/<job_id>", methods=["GET"])
155168
def get_job_status(job_id):
156-
conn = sqlite3.connect("jobs.db")
169+
conn = get_db_connection()
157170
c = conn.cursor()
158171

159172
c.execute(

0 commit comments

Comments
 (0)