from flask import Flask, request, jsonify import threading from processor_worker import run_worker app = Flask(__name__) # 启动后台 Worker(独立线程) worker_thread_started = False def start_worker_once(): global worker_thread_started if not worker_thread_started: worker_thread_started = True t = threading.Thread(target=run_worker, daemon=True) t.start() print("🚀 后台 Worker 已启动(线程模式)") @app.route("/", methods=["GET"]) def health(): start_worker_once() return jsonify({"status": "ok", "message": "Worker running"}) @app.route("/process", methods=["POST"]) def process(): start_worker_once() data = request.json return jsonify({"received": data, "message": "Worker is running in background"}) if __name__ == "__main__": start_worker_once() app.run(host="0.0.0.0", port=7860)