框架
- flask
应用场景
- 当接收到请求,但数据处理比较耗时,希望请求过来时先返回一个响应,再慢慢处理数据,处理完成后再将结果返回给另一个地址。
- 流程:接收到请求,立即返回响应。再处理数据,处理完成后将结果响应给预先定义的URL。
import traceback import requests from concurrent.futures import ThreadPoolExecutor from flask import request, jsonify url = "结果数据要返回的新地址URL" def processing_data(data) print("数据处理...") result_data = "处理完,要返回的数据" try: requests.post(url, data=result_data, timeout=10) except Exception as exc: print(exc) @extra.route("/submit", methods=['POST']) def submit(): data = request.get_data() try: # 创建ThreadPoolExecutor实例 executor = ThreadPoolExecutor(max_workers=3) # 提交任务 future = executor.submit(processing_data, data) # 接收到数据先返回响应,不需要等数据处理完再响应 return jsonify({"status": "true", "messge": "null", "data": "null"}) except Exception: print(traceback.format_exc())
标签:返回,异步,python,submit,响应,线程,print,import,data From: https://www.cnblogs.com/yqw0710/p/18510125