flask 框架 前端和后端请求超时问题
部署模式
flask + Gunicorn + nginx
为什么要用Gunicorn + nginx ?
请看知乎大神们的回答:https://www.zhihu.com/question/38528616
场景
有一项业务,前端给后端发送ajax请求后,后端需要执行大约4分钟的时间才会给前端返回结果,而这时前端已经没有任何反应了.

原因
1. gunicorn超时
2.nginx proxy 超时
解决方法:
1.gunicorn需要配置超时时间,如果不配置,默认为30秒.
意思就是如果后端程序执行时间超过30秒没有结束, 就不会继续执行了,也不会返回值给前端, 后端也没有任何报错.
增加参数 timeout
from flask_script import Command, Option class GunicornServer(Command):
description = 'Run the app within Gunicorn' def __init__(self, host='127.0.0.1', port=5001, workers=50,
worker_class="sync", daemon=False):
self.port = port
self.host = host
self.workers = workers
self.worker_class = worker_class
self.daemon = daemon def get_options(self):
return (
Option('-H', '--host',
dest='host',
default=self.host), Option('-p', '--port',
dest='port',
type=int,
default=self.port), Option('-w', '--workers',
dest='workers',
type=int,
default=self.workers), Option("-c", "--worker_class",
dest='worker_class',
type=str,
default=self.worker_class), Option("-d", "--daemon",
dest="daemon",
type=bool,
default=self.daemon)
) def handle(self, app, host, port, workers, worker_class, daemon): from gunicorn import version_info timeout = 1800 if version_info < (0, 9, 0):
from gunicorn.arbiter import Arbiter
from gunicorn.config import Config arbiter = Arbiter(Config({'bind': "%s:%d" % (host, int(port)),
'workers': workers,
'worker_class': worker_class,
'daemon': daemon,
'timeout': timeout}), app)
arbiter.run()
else:
from gunicorn.app.base import Application class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': '{0}:{1}'.format(host, port),
'workers': workers,
'worker_class': worker_class,
'daemon': daemon,
'timeout': timeout
} def load(self):
return app FlaskApplication().run()
2.修改nginx proxy超时时间,如果不配置,默认60秒
proxy_read_timeout
语法 proxy_read_timeout time
默认值 60s
上下文 http server location
说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。
proxy_send_timeout
语法 proxy_send_timeout time
默认值 60s
上下文 http server location
说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接
根据需要修改时间
[root@locolhost ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx;
worker_processes 1; error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; events {
worker_connections 1024;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_read_timeout 1800;
proxy_send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_send_timeout 1800; #gzip on; # Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf; }
flask 框架 前端和后端请求超时问题的更多相关文章
- 前后端分离框架前端react,后端springboot跨域问题分析
前后端分离框架前端react,后端springboot跨域问题分析 为啥跨域了 前端react的设置 springboot后端设置 为啥跨域了 由于前后端不在一个端口上,也是属于跨域问题的一种,所以必 ...
- Flask框架 (四)—— 请求上下文源码分析、g对象、第三方插件(flask_session、flask_script、wtforms)、信号
Flask框架 (四)—— 请求上下文源码分析.g对象.第三方插件(flask_session.flask_script.wtforms).信号 目录 请求上下文源码分析.g对象.第三方插件(flas ...
- Flask框架(三)—— 请求扩展、中间件、蓝图、session源码分析
Flask框架(三)—— 请求扩展.中间件.蓝图.session源码分析 目录 请求扩展.中间件.蓝图.session源码分析 一.请求扩展 1.before_request 2.after_requ ...
- 【Python】【Flask】前端调用后端方法返回页面
后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...
- 【Python】【Flask】前端调用后端方法
后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...
- 解决前端向后端请求静态资源的问题(基于express框架)
请求js,css,image资源: 前端 <script src='后端url/assets/js/xxx.js'>,<link href='后端url/assets/css/xxx ...
- Flask框架 之上下文、请求钩子与Flask_Script
一.上下文 请求上下文:request与session 应用上下文:current_app与g:一次请求多个函数可以用它传参 @app.route("/") def index() ...
- springboot前端向后端请求返回html语句
后端接口代码 @PostMapping("/service/confirmPay") @ResponseBody public GlobalResponse confirmPay( ...
- Flask框架踩坑之ajax跨域请求
业务场景: 前后端分离需要对接数据接口. 接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比较多)所以在此记录分享我的踩坑之旅,以便能更好的理解,应 ...
随机推荐
- Arduino mega 2560驱动安装失败(没有建立对验证码(TM)签名的目录的发布者信任)的解决方法
转载请注明出处,谢谢...... 放假的时候在自己家台式机上安装时候是很顺畅的,今天在自己本子上安装的时候就不行了~ IDE版本:1.05 问题描述:在网上搜索了相关问题,发现绝大部分安装失败的时候都 ...
- Undefined index: validate(thinkphp)
今天在用thinkphp3.23时发现错误 NOTIC: [8] Undefined index: validate 此处是thinkphp核心目录\Think\Model.class.php 第 ...
- opencv获取像素的值
opencv中获取图像像素的方法 方法一: IplImage *img = cvLoadImage("Lena.jpg", 0); CvScalar pixel; for (int ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- memcahced部署
Memcached是一个内存数据库,数据以key/value键值对的形式保存在服务器预先分配的内存区块中,由于Memcached服务自身没有对缓存的数据进行持久化存储的设计,因此,在服务器端的Memc ...
- 设置参数进行java的jvm监控
1.设置jconsole监控服务器的tomcat参数及java jvm大小,执行命令vi ../tomcat/bin/catalina.sh “在服务器安装的tomcat目录下” JAVA_OPTS ...
- [BZOJ 2743] 采花
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 Algorithm: 此题询问区间内出现次数超过1个的数字 明显在线做无从下手,无 ...
- AOJ 2251 Merry Christmas (最小点覆盖)
[题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251 [题目大意] 给出一张图,现在有一些任务,要求在ti时刻送礼物 ...
- 【kruscal】【最小生成树】【搜索】bzoj1016 [JSOI2008]最小生成树计数
不用Matrix-tree定理什么的,一边kruscal一边 对权值相同的边 暴搜即可.将所有方案乘起来. #include<cstdio> #include<algorithm&g ...
- xcode中一些便捷用法@literals简写
总结一下,新的属性绑定规则如下: ● 除非开发者在实现文件中提供getter或setter,否则将自动生成 ● 除非开发者同时提供getter和setter,否则将自动生成实例变量 ● 只要写了s ...