Flask实现异步非阻塞请求功能实例解析
作者:阏男秀 时间:2022-02-03 14:03:55
本文研究的主要是Flask实现异步非阻塞请求功能,具体实现如下。
最近做物联网项目的时候需要搭建一个异步非阻塞的HTTP服务器,经过查找资料,发现可以使用gevent包。
关于gevent
Gevent 是一个 Python 并发网络库,它使用了基于 libevent 事件循环的 greenlet 来提供一个高级同步 API。下面是代码示例:
from gevent.wsgi import WSGIServer
from yourapplication import app
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
代码清单
下面放上Flask异步非阻塞的代码清单,以后需要用到的时候直接移植即可。
# coding=utf-8
# Python Version: 3.5.1
# Flask
from flask import Flask, request, g
# gevent
from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()
# gevent end
import time
app = Flask(__name__)
app.config.update(DEBUG=True)
@app.route('/asyn/', methods=['GET'])
def test_asyn_one():
print("asyn has a request!")
time.sleep(10)
return 'hello asyn'
@app.route('/test/', methods=['GET'])
def test():
return 'hello test'
if __name__ == "__main__":
# app.run()
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
关于monkey.patch_all()
为什么要加monkey.patch_all()这一条语句呢?在gevnet的官网有详细的解释,这里简单说明一下:
monkey carefully replace functions and classes in the standard socket module with their cooperative counterparts. That way even the modules that are unaware of gevent can benefit from running in a multi-greenlet environment.
翻译:猴子补丁仔细的用并行代码副本替换标准socket模块的函数和类,这种方式可以使模块在不知情的情况下让gevent更好的运行于multi-greenlet环境中。
测试
打开浏览器,首先请求http://127.0.0.1:5000/asyn/,然后
再请求http://127.0.0.1:5000/test/这个接口十次。如果是一般的Flask框架,后面的接口是没有响应的。
打印内容如下:
asyn has a request!
127.0.0.1 - - [2016-10-24 20:45:10] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000998
127.0.0.1 - - [2016-10-24 20:45:13] "GET /test/ HTTP/1.1" 200 126 0.001001
127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.001014
127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.001000
127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:18] "GET /asyn/ HTTP/1.1" 200 126 10.000392
来源:http://blog.csdn.net/yannanxiu/article/details/52915929
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot文件上传控制及Java 获取和判断文件头信息
![](https://img.aspxhome.com/file/2023/1/84771_0s.png)
你什么是Elastic Stack(ELK)
![](https://img.aspxhome.com/file/2023/2/65182_0s.jpg)
详解Spring Boot加载properties和yml配置文件
Java 动态数组的实现示例
![](https://img.aspxhome.com/file/2023/6/63636_0s.gif)
C#采用HttpWebRequest实现保持会话上传文件到HTTP的方法
Kotlin中的惰性操作容器Sequence序列使用原理详解
![](https://img.aspxhome.com/file/2023/7/84717_0s.jpg)
Intellij Idea修改代码方法参数自动提示快捷键的操作
![](https://img.aspxhome.com/file/2023/8/75748_0s.jpg)
SpringBoot如何动态改变日志级别
![](https://img.aspxhome.com/file/2023/2/93152_0s.png)
SpringBoot中 Jackson 日期的时区和日期格式问题解决
![](https://img.aspxhome.com/file/2023/6/67446_0s.png)
java如何将一个float型数的整数部分和小数分别输出显示
解决SpringMvc中普通类注入Service为null的问题
Spring MVC请求参数的深入解析
![](https://img.aspxhome.com/file/2023/3/61573_0s.png)
Java多线程之Semaphore实现信号灯
Spring轻松解决循环依赖
![](https://img.aspxhome.com/file/2023/6/79216_0s.png)
浅析Spring 中 Bean 的理解与使用
浅谈android Fragment横竖屏翻转对重新加载的要求
SpringBoot 过滤器 Filter使用实例详解
新手初学Java集合框架
MyBatis-Plus实现公共字段自动填充功能详解
![](https://img.aspxhome.com/file/2023/2/84242_0s.png)
使用注解@Validated和BindingResult对入参进行非空校验方式
![](https://img.aspxhome.com/file/2023/5/66215_0s.png)