后端调用python遥感功能的方式(How the back end calls Python remote sensing function)

一、BS系统中,java实现后端开发,python实现遥感功能,利用python的flask库将遥感功能发布服务,java通过发送http请求的形式调用python的遥感功能,处理完成后python再通过调用java发布的服务,将处理结果、处理状态等信息通过http请求形式传递给java后端。

from flask import Flask, request
app = Flask(__name__)

@app.route('/algorithm/building_detection', methods=['POST'])
def buildingDectection():
    task_id = request.json.get("taskid", None)
    task_name = request.json.get("taskname", None)
    input_files_str = request.json.get("input_files", None)
    output_file = request.json.get("output_file", None)

    ......

    url = database_url
    data = {"taskid": task_id,
            "taskname": task_name,
            "status": status,
            "dataname": results,
            "error": res_obj.msg}
    data_ = json.dumps(data)
    res = requests.post(url=url, data=data_.encode("utf-8"))


    return dict(res_obj)
if __name__ == '__main__':    app.run(host='0.0.0.0', port=5000, threaded=False) # debug=False,

thread关键字设为True时,可以接收多个http请求,并行处理;设为False时,则是排队逐个处理请求。

注:利用flask发布服务,对应的每个功能都必须有返回值,否则提示“did not return a valid response. The function either returned None or ended without a return statement”

二、遥感功能经常会执行较长时间,发送的http请求可能不能持续等待造成中断,以至于不能接收到处理结果。所以才会在遥感功能处理完成后,再调用后端发布的服务,通过http请求的形式将处理结果、处理状态返回给后端。

三、通过flask发布服务,实现后端调用遥感功能,一旦发送请求后,待处理任务不可控。还有另外一种方法就是利用python去查询数据库中的任务表单,检索到待处理任务后,就获取相关参数,调用对应的遥感算法功能,处理完成后,再将处理结果和处理状态写入数据库。可以用while无线循环查询任务表单,也可以用定时任务定时查询。

if __name__ == '__main__':

    while True:
        handleService = HandleTaskService()
        res = handleService.executeHandleTask()

        time.sleep(30)
————————

1、 In BS system, Java realizes the back-end development and python realizes the remote sensing function. Python’s flash library is used to publish the remote sensing function to the service. Java calls Python’s remote sensing function by sending HTTP request. After processing, python passes the processing result, processing status and other information to the Java back-end through HTTP request by calling the service published by Java.

from flask import Flask, request
app = Flask(__name__)

@app.route('/algorithm/building_detection', methods=['POST'])
def buildingDectection():
    task_id = request.json.get("taskid", None)
    task_name = request.json.get("taskname", None)
    input_files_str = request.json.get("input_files", None)
    output_file = request.json.get("output_file", None)

    ......

    url = database_url
    data = {"taskid": task_id,
            "taskname": task_name,
            "status": status,
            "dataname": results,
            "error": res_obj.msg}
    data_ = json.dumps(data)
    res = requests.post(url=url, data=data_.encode("utf-8"))


    return dict(res_obj)
if __name__ == '__main__':    app.run(host='0.0.0.0', port=5000, threaded=False) # debug=False,

When the thread keyword is set to true, multiple HTTP requests can be received and processed in parallel; When set to false, requests are queued to be processed one by one.

注:利用flask发布服务,对应的每个功能都必须有返回值,否则提示“did not return a valid response. The function either returned None or ended without a return statement”

2、 The remote sensing function often takes a long time to execute, and the sent HTTP request may not wait continuously, resulting in interruption, so that the processing result cannot be received. Therefore, after the remote sensing function processing is completed, call the service published by the back-end, and return the processing results and processing status to the back-end in the form of HTTP request.

3、 The remote sensing function is called by the back end through the flash publishing service. Once the request is sent, the task to be processed is uncontrollable. Another method is to use Python to query the task form in the database. After retrieving the task to be processed, obtain the relevant parameters, call the corresponding remote sensing algorithm function, and write the processing results and processing status into the database after processing. You can use while wireless loop to query the task form, or you can use timed tasks to query regularly.

if __name__ == '__main__':

    while True:
        handleService = HandleTaskService()
        res = handleService.executeHandleTask()

        time.sleep(30)