Using Gevent in flask: API is not asynchronous

后端 未结 3 2050
死守一世寂寞
死守一世寂寞 2021-01-15 23:16

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
fr         


        
相关标签:
3条回答
  • 2021-01-15 23:40

    Not sure, however I think adding thread param in server object can solve the problem.

    http_server = WSGIServer(('127.0.0.1', 8000), app, numthreads=50)

    source: https://f.gallai.re/wsgiserver

    0 讨论(0)
  • 2021-01-15 23:42
    """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
    
    0 讨论(0)
  • 2021-01-15 23:44

    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
    
    0 讨论(0)
提交回复
热议问题