From a9a9fe3e9993a6d8dd3a5b0b0a72068d11f893f9 Mon Sep 17 00:00:00 2001 From: Cp0204 Date: Thu, 16 May 2024 13:26:24 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E8=BF=90=E8=A1=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=B5=81=E5=BC=8F=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/run.py | 31 +++++++++++++++++++---------- app/templates/index.html | 43 ++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/app/run.py b/app/run.py index c698990..7d07815 100644 --- a/app/run.py +++ b/app/run.py @@ -2,14 +2,15 @@ # -*- coding: utf-8 -*- from flask import ( Flask, - render_template, - request, - redirect, url_for, session, jsonify, - send_from_directory, + request, + redirect, Response, + render_template, + send_from_directory, + stream_with_context, ) from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger @@ -140,13 +141,12 @@ def update(): # 处理运行脚本请求 -@app.route("/run_script_now", methods=["POST"]) +@app.route("/run_script_now", methods=["GET"]) def run_script_now(): if not is_login(): return "未登录" - payload = request.json - task_index = str(payload.get("task_index", "")) - command = [python_path, script_path, config_path, task_index] + task_index = request.args.get("task_index", "") + command = [python_path, "-u", script_path, config_path, task_index] def generate_output(): process = subprocess.Popen( @@ -154,11 +154,20 @@ def run_script_now(): stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, + bufsize=1, ) - for line in process.stdout: - yield line + try: + for line in iter(process.stdout.readline, ""): + yield f"data: {line}\n\n" + yield "data: [DONE]\n\n" + finally: + process.stdout.close() + process.wait() - return Response(generate_output(), mimetype="text/plain") + return Response( + stream_with_context(generate_output()), + content_type="text/event-stream;charset=utf-8", + ) # 定时任务执行的函数 diff --git a/app/templates/index.html b/app/templates/index.html index cf816c1..47800eb 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -26,6 +26,11 @@ .title { margin-top: 30px; } + + .modal-body { + max-height: calc(100vh - 200px); + overflow-y: auto; + } @@ -230,7 +235,8 @@