XWeb - 高性能异步 Web 框架。
- 框架使用了高性能的 uvloop+httptools.
- 框架全部采用 python3.6 后的异步 async await 语法.
- 框架架构参考了 koa.js.
优势
- 高性能
- 插件式
- 全异步
Hello World
from xweb import App
app = App()
@app.use
async def response(ctx):
ctx.res.body = "Hello World"
if __name__ == '__main__':
app.listen(8000)
koa.js 官方例子 xweb 版
import time
from xweb import App
app = App()
@app.use
async def logger(ctx, fn):
await fn()
rt = ctx['X-Response-Time']
print(rt)
@app.use
async def response_time(ctx, fn):
start = time.time()
await fn()
usage = (time.time() - start) * 1000_000
ctx['X-Response-Time'] = f'{usage:.0f}µs'
@app.use
async def response(ctx):
ctx.res.body = "Hello World"
if __name__ == '__main__':
app.listen(8000)
性能
- 测试代码目录,为了公平,所有服务都启 4 个进程进行对。benchmarks/
- 环境:
iMac (Retina 4K, 21.5-inch, 2017),3 GHz Intel Core i5,8 GB 2400 MHz DDR4 - 压测命令:
wrk http://127.0.0.1:8000/ -c 100 -t 10 -d 10 -T 10
Requests/Sec:
- xweb 90000
- vibora 90000
- meinheld + wsgi 77000
- sanic 50000
规划(期待有兴趣的同学一起参与进来,创造全异步,高性能的 xweb 插件)
参考 koa.js 的插件开发 xweb 对应的插件,比如
- Body parser
- Data validator
- Router
- Auth
- 等等