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) '''