部署模式

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 框架 前端和后端请求超时问题的更多相关文章

  1. 前后端分离框架前端react,后端springboot跨域问题分析

    前后端分离框架前端react,后端springboot跨域问题分析 为啥跨域了 前端react的设置 springboot后端设置 为啥跨域了 由于前后端不在一个端口上,也是属于跨域问题的一种,所以必 ...

  2. Flask框架 (四)—— 请求上下文源码分析、g对象、第三方插件(flask_session、flask_script、wtforms)、信号

    Flask框架 (四)—— 请求上下文源码分析.g对象.第三方插件(flask_session.flask_script.wtforms).信号 目录 请求上下文源码分析.g对象.第三方插件(flas ...

  3. Flask框架(三)—— 请求扩展、中间件、蓝图、session源码分析

    Flask框架(三)—— 请求扩展.中间件.蓝图.session源码分析 目录 请求扩展.中间件.蓝图.session源码分析 一.请求扩展 1.before_request 2.after_requ ...

  4. 【Python】【Flask】前端调用后端方法返回页面

    后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...

  5. 【Python】【Flask】前端调用后端方法

    后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...

  6. 解决前端向后端请求静态资源的问题(基于express框架)

    请求js,css,image资源: 前端 <script src='后端url/assets/js/xxx.js'>,<link href='后端url/assets/css/xxx ...

  7. Flask框架 之上下文、请求钩子与Flask_Script

    一.上下文 请求上下文:request与session 应用上下文:current_app与g:一次请求多个函数可以用它传参 @app.route("/") def index() ...

  8. springboot前端向后端请求返回html语句

    后端接口代码 @PostMapping("/service/confirmPay") @ResponseBody public GlobalResponse confirmPay( ...

  9. Flask框架踩坑之ajax跨域请求

    业务场景: 前后端分离需要对接数据接口. 接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比较多)所以在此记录分享我的踩坑之旅,以便能更好的理解,应 ...

随机推荐

  1. php必备树状数组处理方法

    thinkphp必备公共方法 /** * 子元素计数器 * @param array $array * @param int $pid * @return array */ function arra ...

  2. SVN的配置和使用

    1.安装前必备 获取 Subversion 服务器程序 到官方网站 http://subversion.tigris.org/    我下的是CollabNetSubversion-server-1. ...

  3. (4)三剑客之awk

    (1)awk工作原理#awk -F: '{print $1,$3}' /etc/passwd 1)awk使用一行作为输入,并将这一行赋给内部变量$0,每一行也可称为一个记录,已换行符结束 2)然后行被 ...

  4. error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'

    http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket- ...

  5. ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】

    题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n ...

  6. 3Sum Smaller -- LeetCode

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. Problem Q: 多项式求和x+(x^2)/2!+(x^3)/3!+...

    #include<stdio.h> #include<math.h> int main() { float x,i; scanf("%f",&x); ...

  8. Saga alternatives – routing slips

    In the last few posts on sagas, we looked at a variety of patterns of modeling long-running business ...

  9. 学习使用常用的windbg命令(u、dt、ln、x)

    http://blog.csdn.net/wesley2005/article/details/51501514 目录: (1) u命令(反汇编) (2) dt命令(查看数据结构) (3) ln命令( ...

  10. Linux下打包命令tar

    转:http://blog.chinaunix.net/uid-29021161-id-3922752.html Linux下最常用的打包程序是tar,用tar命令打成的包文件通常以.tar结尾 1. ...