async
和await
,此后,异步框架迅速发展了起来,性能上能和Node.js比肩,除非是CPU密集型任务,否则没有理由不适用异步框架。
如果你是Web开发者,现在异步Web框架上有了更多选择!
1、Tornado
-
协同程序和其他原语(tornado.gen,tornado.locks,tornado.queues等) -
网络模块(tornado.ioloop,tornado.iostream) -
异步服务器和客户端(tornado.httpserver,httpclient,等)
通过这些模块,Tornado 构建了自己的异步Web框架模块。
<section style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em;white-space: pre;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 0.5em;background: rgb(0, 0, 0);color: rgb(248, 248, 248);text-size-adjust: none;line-height: 1.75em;display: block !important;"><span style="font-size: 16px;"><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">import</span> tornado.ioloop</span><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">import</span> tornado.web</span><br /><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">class</span> <span style="color: rgb(255, 255, 182);">MainHandler</span>(tornado.web.RequestHandler):</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">get</span>(self):</span><br /><span style="letter-spacing: 0.5px;"> self.write(<span style="color: rgb(168, 255, 96);">"Hello, world"</span>)</span><br /><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">make_app</span>():</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> tornado.web.Application([</span><br /><span style="letter-spacing: 0.5px;"> (<span style="color: rgb(168, 255, 96);">r"/"</span>, MainHandler),</span><br /><span style="letter-spacing: 0.5px;"> ])</span><br /><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">if</span> __name__ == <span style="color: rgb(168, 255, 96);">"__main__"</span>:</span><br /><span style="letter-spacing: 0.5px;"> app = make_app()</span><br /><span style="letter-spacing: 0.5px;"> app.listen(<span style="color: rgb(255, 115, 253);">8888</span>)</span><br /><span style="letter-spacing: 0.5px;"> tornado.ioloop.IOLoop.current().start()</span></span></section>
2、 Snaic
async
关键字之外,语法上和 flask 基于没差别。<section style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em;white-space: pre;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 0.5em;background: rgb(0, 0, 0);color: rgb(248, 248, 248);text-size-adjust: none;line-height: 1.75em;display: block !important;"><span style="font-size: 16px;"><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">from</span> sanic <span style="color: rgb(150, 203, 254);">import</span> Sanic</span><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">from</span> sanic.response <span style="color: rgb(150, 203, 254);">import</span> json</span><br /><br /><span style="letter-spacing: 0.5px;">app = Sanic()</span><br /><br /><span style="color: rgb(255, 255, 182);letter-spacing: 0.5px;">@app.route("/")</span><br /><span style="letter-spacing: 0.5px;">async <span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">test</span>(request):</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> json({<span style="color: rgb(168, 255, 96);">"hello"</span>: <span style="color: rgb(168, 255, 96);">"world"</span>})</span><br /><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">if</span> __name__ == <span style="color: rgb(168, 255, 96);">"__main__"</span>:</span><br /><span style="letter-spacing: 0.5px;"> app.run(host=<span style="color: rgb(168, 255, 96);">"0.0.0.0"</span>, port=<span style="color: rgb(255, 115, 253);">8000</span>)</span></span></section>
3、Vibora
Vibora声称比其它框架快几倍,比竞争对手Sanic还快两倍多。当然,这个基准测试要持怀疑态度。
<section style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em;white-space: pre;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 0.5em;background: rgb(0, 0, 0);color: rgb(248, 248, 248);text-size-adjust: none;line-height: 1.75em;display: block !important;"><span style="font-size: 16px;"><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">from</span> vibora <span style="color: rgb(150, 203, 254);">import</span> Vibora, JsonResponse</span><br /><br /><span style="letter-spacing: 0.5px;">app = Vibora()</span><br /><br /><span style="color: rgb(255, 255, 182);letter-spacing: 0.5px;">@app.route('/')</span><br /><span style="letter-spacing: 0.5px;">async <span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">home</span>():</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> JsonResponse({<span style="color: rgb(168, 255, 96);">'hello'</span>: <span style="color: rgb(168, 255, 96);">'world'</span>})</span><br /><br /><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">if</span> __name__ == <span style="color: rgb(168, 255, 96);">'__main__'</span>:</span><br /><span style="letter-spacing: 0.5px;"> app.run(host=<span style="color: rgb(168, 255, 96);">"0.0.0.0"</span>, port=<span style="color: rgb(255, 115, 253);">8000</span>)</span></span></section>
4、Quart
<section style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em;white-space: pre;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 0.5em;background: rgb(0, 0, 0);color: rgb(248, 248, 248);text-size-adjust: none;line-height: 1.75em;display: block !important;"><span style="font-size: 16px;"><span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">from</span> quart <span style="color: rgb(150, 203, 254);">import</span> Quart</span><br /><br /><span style="letter-spacing: 0.5px;">app = Quart(__name__)</span><br /><br /><span style="color: rgb(255, 255, 182);letter-spacing: 0.5px;">@app.route('/')</span><br /><span style="letter-spacing: 0.5px;">async <span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">hello</span>():</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> <span style="color: rgb(168, 255, 96);">'hello'</span></span><br /><br /><span style="letter-spacing: 0.5px;">app.run()</span></span></section>
5、FastAPI
<section style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em;white-space: pre;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);padding: 0.5em;background: rgb(0, 0, 0);color: rgb(248, 248, 248);text-size-adjust: none;line-height: 1.75em;display: block !important;"><span style="font-size: 16px;"><span style="letter-spacing: 0.5px;">rom fastapi <span style="color: rgb(150, 203, 254);">import</span> FastAPI</span><br /><br /><span style="letter-spacing: 0.5px;">app = FastAPI()</span><br /><br /><span style="color: rgb(255, 255, 182);letter-spacing: 0.5px;">@app.get("/users/me")</span><br /><span style="letter-spacing: 0.5px;">async <span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">read_user_me</span>():</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> {<span style="color: rgb(168, 255, 96);">"user_id"</span>: <span style="color: rgb(168, 255, 96);">"the current user"</span>}</span><br /><br /><span style="color: rgb(255, 255, 182);letter-spacing: 0.5px;">@app.get("/users/{user_id}")</span><br /><span style="letter-spacing: 0.5px;">async <span style="color: rgb(150, 203, 254);">def</span> <span style="color: rgb(255, 255, 182);">read_user</span>(user_id: str):</span><br /> <span style="letter-spacing: 0.5px;"><span style="color: rgb(150, 203, 254);">return</span> {<span style="color: rgb(168, 255, 96);">"user_id"</span>: user_id}</span></span></section>
总结
近期热门:
学会Python这项技能,再也不怕你孩子偷偷玩电脑游戏了
过年扫五福,干脆我用Python做一个五福生成器
菜鸟学Python 趣味干货视频已经上线B站
本篇文章来源于: 菜鸟学Python
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 神器推荐!一个能监控文件变化的Python神器—看门狗07/14
- ♥ 对比6款 “数据分析” 工具,学什么,一目了然!07/20
- ♥ python如何接受参数12/02
- ♥ 如何在python中选择32位或64位09/19
- ♥ Python 包括哪 4 种数字类型?09/07
- ♥ Queue在python中是如何通信的12/18
内容反馈