Fix Flask app context error in SSE stream route
This commit is contained in:
35
app.py
35
app.py
@@ -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)
|
||||
|
||||
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
|
||||
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 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')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user