Python Flask 高并发部署(gevent)

00x1 安装gevent

pip install gevent

00x2 修改 flask

#! -*- coding:utf-8 -*-

from flask import Flask, jsonify
from gevent.wsgi 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()

00x3 测试

#! -*- 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)

赞 (0)

评论区

发表评论

3+46=?

暂无评论,要不来一发?

回到顶部