如果我为前一个请求调用的函数尚未完成,如何忽略Flask中的POST请求

时间:2017-06-14 18:40:41

标签: python flask

我有以下Flask代码

app = Flask(__name__)

@app.route("/get_bounding_box", methods=['GET', 'POST'])
def hello():
    res = call_another_func()

    return "False"



if __name__ == "__main__":
        app.run(debug=True, host='10.0.10.15')

call_another_func()函数执行一些重负载处理。

与此同时,当call_another_func()忙于处理上一个请求时,如果另一个请求通过,我只想忽略该请求并等待该函数完成其处理。

如何在Flask中实现此功能?

1 个答案:

答案 0 :(得分:1)

为什么不根据请求使用简单的互斥锁和返回状态,而不是忽略它?

app = Flask(__name__)
import threading
call_another_func_lock = threading.Lock()

@app.route("/get_bounding_box", methods=['GET', 'POST'])
def hello():
    if call_another_func_lock.acquire(False):
        res = call_another_func()
        call_another_func_lock.release()
        return "False"
    else:
        return "call_another_func is not finished yet"

if __name__ == "__main__":
        app.run(debug=True, host='10.0.10.15')