Python POST请求处理导致Spring-boot关闭连接

时间:2019-05-28 12:54:23

标签: java python spring-boot flask nltk

我正在构建Spring-Boot Web应用程序,并在POST中实现以下流程前端-> Spring-boot后端-> Flask服务器-> Spring-Boot后端->前端,但是在处理我收到的数据时,与Flask服务器的连接会关闭。

春季启动:

@PostMapping(value = "/home")
    public ResponseEntity<List<BookDTO>> getSearchMessage(@RequestBody MessageDTO data) {
        logger.info("Message received from frontend {}", data.getMessage());
        String url = "http://localhost:5000/python";
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, data, String.class);
        logger.info("Response from python {}", responseEntity.getBody());
        return ResponseEntity.ok("Python returned " + responseEntity.getBody());

Flask Server通过调用一个函数来处理请求,该函数返回保存在列表中(接收到的消息与我使用NLTK和Word2Vec在数据库中存储的某些数据之间)的相似性分数,并且应该仅将其在响应中发送回我后端。 (我没有在代码中包括所需的导入)

def create_app():
    app = Flask(__name__)
    def run_on_start(*args, **argv):
        print("--------------------------------")
        global data
        try:
            data = processData()
            print("Success on processing data")
        except Exception as e:
            print(e)
        print("--------------------------------")

        global model
        try:
            model = loadModel()
            print("Success on loading model")
        except Exception as e:
            print(e)
        print("--------------------------------")
    run_on_start()
    return app

app = create_app()

@app.route('/python', methods=['POST'])
def processRequest():
    if not request.json:
        abort(400)
    reqMessage = request.json
    print("Input =", reqMessage['message'])
    bookIds = returnScore(reqMessage['message'], data, model)
    return json.dumps(bookIds)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

由于函数returnScore(reqMessage['message'], data, model)需要花费大量时间来处理和返回,我相信这就是为什么它导致连接关闭并在Spring-Boot中引发此错误的原因:

2019-05-28 15:49:32.478 ERROR 6316 --- [nio-7090-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:5000/python": Connection reset; nested exception is java.net.SocketException: Connection reset] with root cause

java.net.SocketException: Connection reset

我已经通过简单地检索一个硬编码列表在python中测试了POST,它可以正常工作,所以我猜问题是由我上面提到的引起的,我不知道该如何解决。

感谢您的帮助。

0 个答案:

没有答案