从 angularjs 加载 REST 资源时等待时间非常长 [英] Extremely long wait time when loading REST resource from angularjs

查看:29
本文介绍了从 angularjs 加载 REST 资源时等待时间非常长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个访问 Flask/python RESTful API 的 angular 前端.我使用的是 AngularJS v1.2.16.

I'm building a front-end in angular that is accessing a flask/python RESTful API. I'm using AngularJS v1.2.16.

出于某种原因,在加载 REST 资源之前需要花费大量时间,大部分时间只是在等待.我的理解是等待"是测量到第一个字节的时间——我所有的服务都在本地运行(前端、API 和数据库).

For some reason, it takes an insane amount of time before the REST resource is loaded, with most of the time just waiting. It's my understanding that 'waiting' is measuring the time to first byte - all my services run locally (the frontend, API and database).

鉴于服务都在本地运行,我不知道如何调试.有没有人有任何关于在哪里看的提示?我检查了我所有的方法,它们运行得非常快(每个 REST 调用不到 100 毫秒).当我使用 postman 时,API 返回近-即刻.

Given that the services all run locally, I am at a loss how to debug this. Does anybody have any tips on where to look? I checked all my methods and they run decently fast (under 100ms per REST call). When I use postman, the API returns near-instantly.

关于如何修复等待的任何想法,似​​乎只有通过 angular 加载 RESTful 资源时才会出现这种情况.有角度的 $http get 请求非常简单:

Any ideas how to fix the wait, it only seems to be the case when loading the RESTful resource via angular. The angular $http get request is fairly straight forward:

myAppControllers.controller('ManageCtrl', ['$scope', '$http',
    function($scope, $http) {
        $http({
            url: 'http://127.0.0.1:5000/v1/domains/',
            method: "GET",
            headers: { 'Content-Type': 'application/json' },
        }).
        success(function(data, status, headers, config) {
            console.log('login successful');
            console.log(status);
            console.log(data);
        }).
        error(function(data, status, headers, config) {
            console.log('login failed');
        });
    }]);

  • 该问题仅出现在 Google Chrome 的常规模式中.
  • 使用隐身模式时 GET 请求速度很快.

推荐答案

我们遇到了同样的问题,经过一些研究,问题与 Chrome 使用连接的方式和 Flask 默认配置为单线程有关.

We had the same problem and after some research the problem is related with the way Chrome uses connections and the default configuration of flask as mono threaded.

在flask应用程序启动时添加threaded=true解决了问题:

Added threaded=true when flask app is started solved the problem:

app.run(threaded=True)

这篇关于从 angularjs 加载 REST 资源时等待时间非常长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆