如何使用Flask实现异步非阻塞HTTP请求功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计403个文字,预计阅读时间需要2分钟。
安装Gevent:pip install geventGevent简介:Gevent是一个Python并发网络库,利用libevent库和greenlet来实现异步I/O,提供高级同步API。
代码示例:pythonfrom gevent.wsgi import WSGIServerfrom yourapplication import application
http_server=WSGIServer(('', 8000), application)http_server.serve_forever()
pip install gevent关于gevent
Gevent 是一个 Python 并发网络库,它使用了基于 libevent 事件循环的 greenlet 来提供一个高级同步 API。下面是代码示例:
from gevent.wsgi import WSGIServerfrom yourapplication import app
127.0.0.1:5000/asyn/,然后
再请求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
本文共计403个文字,预计阅读时间需要2分钟。
安装Gevent:pip install geventGevent简介:Gevent是一个Python并发网络库,利用libevent库和greenlet来实现异步I/O,提供高级同步API。
代码示例:pythonfrom gevent.wsgi import WSGIServerfrom yourapplication import application
http_server=WSGIServer(('', 8000), application)http_server.serve_forever()
pip install gevent关于gevent
Gevent 是一个 Python 并发网络库,它使用了基于 libevent 事件循环的 greenlet 来提供一个高级同步 API。下面是代码示例:
from gevent.wsgi import WSGIServerfrom yourapplication import app
127.0.0.1:5000/asyn/,然后
再请求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

