Ali Raja
Ali Raja

Reputation: 53

How enable/implement Multi Threading in the WSGIServer of Flask Python

I have a Flask API which servers to Web and Mobile Apps. But sometimes on heavy load, app or websites stop quick response and displays results taking time, I just want to enable multithreading in the flask running with WSGIServer.

def main():
"""Main entry point of the app."""
try:
    http_server = WSGIServer(('0.0.0.0', 8084), app, log=logging, error_log=logging)
    http_server.serve_forever()
except Exception as exc:
    logger.error(exc.message)
    logger.exception(traceback.format_exc())
finally:
    # Do something here
    pass

Thanks,

Upvotes: 1

Views: 2338

Answers (1)

amitchone
amitchone

Reputation: 1638

The built-in Flask development server, whilst not intended for multithreaded use or deployment does allow multithreading:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, world!'

if __name__ == '__main__':
    app.run(threaded=True)

The above code is a simple Hello World script that uses multithreading; not that any process is using another thread, but you get the idea.

Upvotes: 2

Related Questions