54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
# app.py
|
|
|
|
from flask import Flask, render_template, jsonify
|
|
from multiprocessing import Process, Queue
|
|
import time
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Queue to store task results
|
|
task_queue = Queue()
|
|
task_process = None
|
|
|
|
|
|
def long_running_task():
|
|
# Simulate a long-running task
|
|
time.sleep(5)
|
|
# Store the result in the task queue
|
|
task_queue.put("Task completed")
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('/start-task')
|
|
def start_task():
|
|
global task_process
|
|
|
|
if task_process is None or not task_process.is_alive():
|
|
# Start the long-running task in a separate process
|
|
task_process = Process(target=long_running_task)
|
|
task_process.start()
|
|
return jsonify({'status': 'Task started'})
|
|
else:
|
|
return jsonify({'status': 'Task already in progress'})
|
|
|
|
|
|
@app.route('/task-status')
|
|
def task_status():
|
|
# Check the status of the task
|
|
if task_process is not None and task_process.is_alive():
|
|
status = 'In progress'
|
|
elif not task_queue.empty():
|
|
status = task_queue.get()
|
|
else:
|
|
status = 'Task not started'
|
|
|
|
return jsonify({'status': status})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run()
|