Python Flask 高并发部署(简易)

一、安装gevent

pip install gevent

二、修改 flask

#! -*- coding:utf-8 -*-
 
from flask import Flask, jsonify
from gevent.pywsgi import WSGIServer #关键这个
 
app = Flask(__name__)
 
#这里的json使用中文key
@app.route("/", methods=['GET', 'POST'])
def index():
    return jsonify({'ret':'hi'})
 
WSGIServer(('127.0.0.1', 5000), app).serve_forever()

三、测试

#! -*- coding:utf-8 -*-
 
import urllib
import json
import time
 
url = 'http://127.0.0.1:5000/'
 
while True:
    t1 = time.time()
    print(t1)

    #并不能保证每一次请求都能返回结果。必需设置timeout
    res = json.loads(urllib.request.urlopen(url, timeout=10).read().decode('utf-8'))
    
    dt = time.time() - t1
    print("耗时"+"%.2f秒" % dt)
    print(res)
    time.sleep(0.01)

 

你可能感兴趣的:(python,部署)