title: Architecture Flask vs FastAPI
body: <p>I have been tinkering around Flask and FastAPI to see how it acts as a server.<br />
  One of the main things that I would like to know is how Flask and FastAPI deal with multiple requests from multiple clients.<br />
  Especially when the code has efficiency issues (long database query time).</p>
  <p>So, I tried making a simple code to understand this problem.<br />
  The code is simple, when the client access the route, the application sleeps for 10 seconds before it returns results.<br />
  It looks something like this:</p>
  <p><strong>FastAPI</strong></p>
  <pre><code>import uvicorn
  from fastapi import FastAPI
  from time import sleep
  app = FastAPI()
  
  @app.get('/')
  async def root():
      print('Sleeping for 10')
      sleep(10)
      print('Awake')
      return {'message': 'hello'}
  
  if __name__ == &quot;__main__&quot;:
      uvicorn.run(app, host=&quot;127.0.0.1&quot;, port=8000)
  </code></pre>
  <p><strong>Flask</strong></p>
  <pre><code>from flask import Flask
  from flask_restful import Resource, Api
  from time import sleep
  
  app = Flask(__name__)
  api = Api(app)
  
  class Root(Resource):
      def get(self):
          print('Sleeping for 10')
          sleep(10)
          print('Awake')
          return {'message': 'hello'}
  
  api.add_resource(Root, '/')
  
  if __name__ == &quot;__main__&quot;:
      app.run()
  </code></pre>
  <p>Once the applications are up, I tried accessing them at the same time through 2 different chrome clients.
  The below are the results:</p>
  <p><strong>FastAPI</strong></p>
  <p><img src="https://i.stack.imgur.com/z7vtH.png" alt="enter image description here" /></p>
  <p><strong>Flask</strong></p>
  <p><img src="https://i.stack.imgur.com/zabMr.png" alt="enter image description here" /></p>
  <p>As you can see, for FastAPI, the code first waits 10 seconds before processing the next request. Whereas for Flask, the code processes the next request while the 10-second sleep is still happening.</p>
  <p>Despite doing a bit of googling, there is not really a straight answer on this topic.<br />
  If anyone has any comments that can shed some light on this, please drop them in the comments.</p>
  <p>Your opinions are all appreciated. Thank you all very much for your time.</p>
  <p><strong>EDIT</strong>
  An update on this, I am exploring a bit more and found this concept of Process manager. For example, we can run uvicorn using a process manager (gunicorn). By adding more workers, I am able to achieve something like Flask. Still testing the limits of this, however.
  <a href="https://www.uvicorn.org/deployment/" rel="noreferrer">https://www.uvicorn.org/deployment/</a></p>
  <p>Thanks to everyone who left comments! Appreciate it.</p>