zengbin93 / blog

17 stars 10 forks source link

实践 - Python - API设计开发 #26

Open zengbin93 opened 6 years ago

zengbin93 commented 6 years ago

API常用的类型有两种,分别是:Restful API和 json-rpc。

zengbin93 commented 6 years ago

使用flask设计Restful API

参考资料

使用gevent 部署 flask app

gevent使用一个线程跑多个携程,适用于频繁的I/O操作,如果CPU操作较多,时间上不好控制。此外,gevent不会缩短原始操作的时间,只会增多。引入gevent的好处是原来30个/s的并发有可能提高到100个/s。

from flask import Flask, jsonify  
from gevent.wsgi import WSGIServer  

app = Flask(__name__)  

@app.route("/", methods=['GET', 'POST'])  
def index():  
    return jsonify({'ret':'hi'})  

WSGIServer(('0.0.0.0', 8889), app).serve_forever()

问题记录

zengbin93 commented 6 years ago

gunicorn - a Python WSGI HTTP Server for UNIX

image