Fix Flask app context error in SSE stream route

This commit is contained in:
2026-03-21 01:30:57 +01:00
parent 5860e4634b
commit ee0b0f3abd

33
app.py
View File

@@ -358,26 +358,29 @@ def api_download():
@login_required
def stream_download(download_id):
download = Download.query.get_or_404(download_id)
download_id_val = download.id
def generate():
last_len = 0
while True:
db.session.refresh(download)
current_len = len(download.log)
with app.app_context():
download = Download.query.get(download_id_val)
last_len = 0
while True:
db.session.refresh(download)
current_len = len(download.log)
if current_len > last_len:
new_log = download.log[last_len:]
data = f"data: {download.id}|{download.status}|{download.progress}|{new_log}\n\n"
yield data
last_len = current_len
if current_len > last_len:
new_log = download.log[last_len:]
data = f"data: {download.id}|{download.status}|{download.progress}|{new_log}\n\n"
yield data
last_len = current_len
if download.status in ('completed', 'error'):
data = f"data: {download.id}|{download.status}|{download.progress}|__END__\n\n"
yield data
break
if download.status in ('completed', 'error', 'cancelled'):
data = f"data: {download.id}|{download.status}|{download.progress}|__END__\n\n"
yield data
break
import time
time.sleep(0.5)
import time
time.sleep(0.5)
return Response(generate(), mimetype='text/event-stream')