Blog

Blog

PHODAL

Peewee falcon打造高性能python RESTful服务(一)——Falcon初入

之前用nodejs打造过一个编辑发布分离的博客。想着从头打造一个编辑发布分离网站,才发现原来自己的时间没有那么充足。于是先从打造一个RESTful做起,。。

RESTful 性能对比

网上找了一个RESTful框架的性能对比,发现falcon已经表现得不错了。至少是在python中是最好的

Framework best vs Platform best Ratio
gemini 914,749 vs servlet 831,515 110.0%
start 51,235 vs dart 54,896 93.3%
stream 50,310 vs dart 54,896 91.6%
beego 297,377 vs go 348,555 85.3%
revel 284,683 vs go 348,555 81.7%
falcore 274,991 vs go 348,555 78.9%
falcon 171,773 vs wsgi 220,602 77.9%
ringojs-conv 82,404 vs ringojs 106,037 77.7%
bottle 159,153 vs wsgi 220,602 72.1%

再看看官网的一个对比

Framework|req/sec|μs/req|Performance ------|-------|------|--|-- Falcon|(0.1.8)|24,358|41|8x Bottle|(0.11.6)|13,623|73|4x Pecan|(0.4.4)|6,604|151|2x Werkzeug|(0.9.4)|5,163|194|2x Flask|(0.10.1)|3,041|329|1x

虽然没有看懂,但是这个意思明显就是这个框架性能不错。

Falcon Framework

官网有这么一个简介没有看懂

Unburdening cloud apps for over 2.07 x 10-2 centuries.

falcon安装

pip install falcon

有下面的结果:

Downloading/unpacking falcon
  Downloading falcon-0.1.10.tar.gz (59kB): 59kB downloaded
  Running setup.py (path:/Users/fdhuang/test/build/falcon/setup.py) egg_info for package falcon

    WARNING: Cython not installed. Falcon modules will still work fine, but will run a bit slower.


Requirement already satisfied (use --upgrade to upgrade): six in /Users/fdhuang/test/lib/python2.7/site-packages (from falcon)
Requirement already satisfied (use --upgrade to upgrade): python-mimeparse in /Users/fdhuang/test/lib/python2.7/site-packages (from falcon)
Installing collected packages: falcon
  Running setup.py install for falcon

    WARNING: Cython not installed. Falcon modules will still work fine, but will run a bit slower.


    Installing falcon-bench script to /Users/fdhuang/test/bin
Successfully installed falcon
Cleaning up...

才意识到我要先安装cython,这就是为什么这货速度这么快的原因么?

  WARNING: Cython not installed. Falcon modules will still work fine, but will run a bit slower.

安装Cython

pip install cython

falcon示例

官方有一个简单的示例

import falcon


class ThingsResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.body = ('\nI've always been more interested in\n
                     'the future than in the past.\n'
                     '\n'
                     '    ~ Grace Hopper\n\n')

app = falcon.API()
things = ThingsResource()
app.add_route('/things', things)

和flask看上去区别不大,后面发现区别真tmd大。

如果我们的这个server.py是基于flask的话,那么:

python server.py

就可以启动我们地server,于是我试了试,发现什么也没有返回。。

falcon gunicorn示例

官网是这样子写到的

The falcon.API class instances are WSGI applications and can be run with any WSGI server

我们要这样去写

 gunicorn things:app

瞬间觉得ciao了,也只能

gunicorn server:app

去运行了。。。

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签