深入flask之异步非堵塞实现代码示例
作者:danny_amos 时间:2021-12-27 17:37:25
官方其实已经给出了方案,只不过藏的有点深,在加上网上有很多不太靠谱的帖子误导了我(当然不排除我没理解的原因哈)。所以为了让有些朋友的少走点弯路,也为给自己做个备忘。
完整代码:https://github.com/wskssau/my_notespace的 python/todo_app
解决方案: flask+gevent
安装gevent
pip install gevent
修改代码
# 文件头部
from gevent import monkey
from gevent.pywsgi import WSGIServer
# 在玩websockets,可以无视之哈,有空贴下flask websockets实现哈
from geventwebsocket.handler import WebSocketHandler
import time
#gevent的猴子魔法
monkey.patch_all()
app = Flask(__name__)
app.config.update(
DEBUG=True
)
@app.route('/asyn/1/', methods=['GET'])
def test_asyn_one():
if request.method == 'GET':
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, handler_class=WebSocketHandler)
http_server.serve_forever()
运行之后可以先访问/asyn/1/再访问/test/,可以明显发现,/asyn/1/在做耗时任务时不会影响其他请求
来源:https://blog.csdn.net/danny_amos/article/details/50859383
标签:flask,异步,非堵塞
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python time.time()方法
2022-11-27 16:55:47
![](https://img.aspxhome.com/file/2023/6/127276_0s.png)
Python使用OpenPyXL处理Excel表格
2022-07-29 21:10:21
![](https://img.aspxhome.com/file/2023/1/87791_0s.png)
python 实现快速生成连续、随机字母列表
2021-02-20 19:46:45
python多个模块py文件的数据共享实例
2022-02-24 23:33:39
简单了解Python中的几种函数
2023-09-30 06:11:55
一个ASP记录集分页显示的例子
2007-09-14 10:57:00
使用python进行图片的文字识别详细代码
2021-06-27 07:01:06
![](https://img.aspxhome.com/file/2023/6/101336_0s.jpg)
分享10个Js的小型库,效果真的很棒
2009-08-27 15:38:00
Python变量名详细规则详细变量值介绍
2021-08-05 07:51:57
![](https://img.aspxhome.com/file/2023/9/77589_0s.png)
软件与网站设计的区别
2009-05-04 14:30:00
Python iter()函数用法实例分析
2022-11-01 00:00:01
Python使用sorted排序的方法小结
2022-09-03 21:11:23
![](https://img.aspxhome.com/file/2023/5/112975_0s.jpg)
安装并免费使用Pycharm专业版(学生/教师)
2023-09-27 08:14:13
![](https://img.aspxhome.com/file/2023/4/94054_0s.png)
Oracle 存储过程加密方法
2009-10-23 18:02:00
python在Windows8下获取本机ip地址的方法
2023-07-31 17:10:44
js检查全角字符正则表达式[\\uFE30-\\uFFA0]
2008-10-30 12:39:00
IE6中隐形的PNG8图片
2009-11-27 18:38:00
![](https://img.aspxhome.com/file/UploadPic/200911/27/01-15s.png)
Python 用NumPy创建二维数组的案例
2021-11-23 22:27:48
![](https://img.aspxhome.com/file/2023/7/99147_0s.jpg)
python DataFrame中loc与iloc取数据的基本方法实例
2022-06-23 15:58:25
![](https://img.aspxhome.com/file/2023/8/67868_0s.png)
Python统计时间内的并发数代码实例
2022-02-17 18:24:16