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

标签:python,flask,异步非阻塞
0
投稿

猜你喜欢

  • SpringBoot文件上传控制及Java 获取和判断文件头信息

    2021-12-10 16:47:20
  • 你什么是Elastic Stack(ELK)

    2022-12-02 20:12:43
  • 详解Spring Boot加载properties和yml配置文件

    2023-11-24 07:14:09
  • Java 动态数组的实现示例

    2022-02-27 07:05:25
  • C#采用HttpWebRequest实现保持会话上传文件到HTTP的方法

    2023-07-18 17:20:29
  • Kotlin中的惰性操作容器Sequence序列使用原理详解

    2023-10-01 14:21:55
  • Intellij Idea修改代码方法参数自动提示快捷键的操作

    2022-11-19 08:08:37
  • SpringBoot如何动态改变日志级别

    2021-08-29 02:32:17
  • SpringBoot中 Jackson 日期的时区和日期格式问题解决

    2021-09-06 19:37:50
  • java如何将一个float型数的整数部分和小数分别输出显示

    2022-08-17 16:50:26
  • 解决SpringMvc中普通类注入Service为null的问题

    2023-10-29 01:23:25
  • Spring MVC请求参数的深入解析

    2021-11-26 22:55:25
  • Java多线程之Semaphore实现信号灯

    2023-09-19 23:04:51
  • Spring轻松解决循环依赖

    2021-11-07 15:06:11
  • 浅析Spring 中 Bean 的理解与使用

    2023-07-09 03:12:03
  • 浅谈android Fragment横竖屏翻转对重新加载的要求

    2023-07-27 21:55:28
  • SpringBoot 过滤器 Filter使用实例详解

    2021-11-08 13:46:00
  • 新手初学Java集合框架

    2022-10-06 03:01:51
  • MyBatis-Plus实现公共字段自动填充功能详解

    2021-10-19 22:14:39
  • 使用注解@Validated和BindingResult对入参进行非空校验方式

    2022-09-16 11:30:44
  • asp之家 软件编程 m.aspxhome.com