플라스크에서 Gevent 사용 : API가 비동기가 아닙니다.
Aug 18 2020
이전에는 웨이트리스를 사용했습니다. 이제 Gevent 를 사용하여 API가 하나만있는 Flask 앱을 실행하고 있습니다.
from flask import Flask, request, jsonify
import documentUtil
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
@app.route('/post-document-string', methods=['POST'])
def parse_data():
req_data = request.get_json(force=True)
text = req_data['text']
result = documentUtil.parse(text)
return jsonify(keywords = result)
if __name__=='__main__':
http_server = WSGIServer(('127.0.0.1', 8000), app)
http_server.serve_forever()
이것은 잘 작동합니다. 그러나 API는 비동기식이 아닙니다. 프런트 엔드에서 동일한 API를 동시에 두 번 실행하면 두 번째 호출은 첫 번째 호출이 먼저 응답을 줄 때까지 기다립니다.
여기서 무엇이 잘못 되었습니까? 어떻게 비 동기화 할 수 있습니까?
답변
1 AlexWeavers Sep 01 2020 at 14:30
우리는 Gunicorn을 사용하여 여러 프로세스에서 Flask를 실행합니다. 그런 식으로 파이썬에서 더 많은 주스를 얻습니다 + 자동 재시작 등. 샘플 구성 파일 :
import multiprocessing
bind = "0.0.0.0:80"
workers = (multiprocessing.cpu_count() * 2) + 1
# ... additional config
그런 다음 다음과 같이 실행하십시오.
gunicorn --config /path/to/file application.app
1 pritesh Sep 07 2020 at 06:43
확실하지 않지만 서버 개체에 스레드 매개 변수 를 추가 하면 문제를 해결할 수 있다고 생각 합니다.
http_server = WSGIServer(('127.0.0.1', 8000), app, numthreads=50)
출처: https://f.gallai.re/wsgiserver
1 EM28 Sep 07 2020 at 23:17
"""index.py"""
from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route('/')
def index():
"""Main page"""
doc = {
'site': 'stackoverflow',
'page_id': 6347182,
'title': 'Using Gevent in flask'
}
return jsonify(doc)
# To start application
gunicorn -k gevent --bind 0.0.0.0 index:app
k : worker_class
--bind : bind address
# See https://docs.gunicorn.org/en/latest/settings.html