Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Gevent in flask: API is not asynchronous

Earlier I was using Waitress. Now I'm using Gevent to run my Flask app that has only one API

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()

This works fine. But the API is not asynchronous. If from front-end, I fire the same API twice at the same time, the second call waits for the first one to give response first.

What is wrong here ? How can I make it asynchronous ?

like image 343
Gissipi_453 Avatar asked Oct 28 '25 17:10

Gissipi_453


1 Answers

We use Gunicorn to run Flask in multiple processes. You get more juice out of python that way + auto restarts and stuff. Sample config file:

import multiprocessing

bind = "0.0.0.0:80"
workers = (multiprocessing.cpu_count() * 2) + 1
# ... additional config

Then run with something like

gunicorn --config /path/to/file application.app
like image 115
Alex Weavers Avatar answered Oct 31 '25 20:10

Alex Weavers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!