diff --git a/localcache.py b/localcache.py index a58f8aa..a42f256 100644 --- a/localcache.py +++ b/localcache.py @@ -1852,6 +1852,12 @@ WHERE connection.close() +def test_long_running(): + from time import sleep + print("Starting long process...") + for i in range(20): + print("sleeping %s" % i, flush=True) + sleep(1) @@ -1881,6 +1887,7 @@ if __name__ == "__main__": 20: ['Process enrollment data', process_enrollment_data], 21: ['Encode data', do_encoding], 22: ['all students course history', all_students_history], + 23: ['test long running', test_long_running], #19: ['add evals for a whole semester', instructor_list_to_activate_evals], #16: ['Upload new employees to flex app', employees_refresh_flex], } diff --git a/myweb.py b/myweb.py new file mode 100644 index 0000000..f7eaa8f --- /dev/null +++ b/myweb.py @@ -0,0 +1,77 @@ +from flask import Flask, render_template +from flask_socketio import SocketIO, emit +from flask_sse import sse + +from threading import Thread +import subprocess + + + +from flask import Flask, render_template +from flask_sse import sse +from threading import Thread +import subprocess + +app = Flask(__name__) +app.config['REDIS_URL'] = 'redis://localhost' +app.register_blueprint(sse, url_prefix='/stream') + +# Background thread to run the long-running task +def run_long_running_task(): + process = subprocess.Popen(['python', 'localcache.py', '23'], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True) + for line in process.stdout: + # Emit the output line as a server-sent event + sse.publish({'data': line.strip()}, type='output') + process.wait() + +@app.route('/') +def index(): + return render_template('myweb.html') + +@app.route('/start') +def start_task(): + # Start the long-running task in a background thread + thread = Thread(target=run_long_running_task) + thread.start() + return 'Task started' + +if __name__ == '__main__': + app.run() + + + + +''' +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret_key' +socketio = SocketIO(app) + +# Background process to run the long-running task +def run_long_running_task(): + process = subprocess.Popen(['python', 'localcache.py', '23'], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True) + for line in process.stdout: + # Emit the output line to the client + socketio.emit('output', {'data': line.strip()}) + print(f"Sent: {line.strip()}") + print("Process is done") + process.wait() + +@app.route('/') +def index(): + return render_template('myweb.html') + +@socketio.on('connect') +def on_connect(): + # Start the long-running task when a client connects + thread = Thread(target=run_long_running_task) + thread.start() + +if __name__ == '__main__': + socketio.run(app) +''' \ No newline at end of file diff --git a/templates/myweb.html b/templates/myweb.html new file mode 100644 index 0000000..8c74a7c --- /dev/null +++ b/templates/myweb.html @@ -0,0 +1,47 @@ + + + + Interactive Text Program + + + + +
+ + + +