Quart the Asyncio web framework

sparkidev
2 min readOct 18, 2019

Node.js has changed the JS world forever, So as a python developer at that time I thought hey why can’t we bring that architecture to python (we already had generators ), even though it was a bit late the asyncio feature was released on the 3.4 version.

So what does it do, compare to JS async/await statement

Let us see an example of how asyncio works in python

#!/usr/bin/env python3
# countasync.py

import asyncio

async def saySomething():
print("Hai")
await asyncio.sleep(1)
print("Ok Bye")

async def main():
await asyncio.gather(saySomething(), saySomething(), saySomething())

if __name__ == "__main__":

asyncio.run(main())

ok, so what happens when we run the program

Output
----------------------------------------------------------------

Hai
Hai
Hai
Ok Bye
Ok Bye
Ok Bye

Now we are seeing something that we have only seen in a browser console. This is now officially supported in python 😌

So the heading was about Quart?

Let’s get down with what Quart is, to be frank, its an exact fork of Flask the minimal web framework for python, for the past few months I have been working on a project to do some System-Level I/O stuff,

It has proved its worth on Event-Driven systems such and the benchmark was so promising and the performance is notable.

3X faster than flask to be exact with minimal response time’s (Tested with PostgreSQL)

Route           | Requests per second | Average Latency [ms]  |
| Flask | Quart | Flask | Quart |
---------------------------------------------------------------
GET /films/995/ | 330.22 | 1160.27 | 60.55 | 17.23 |
GET /films/ | 99.39 | 194.58 | 201.14 | 102.76 |
POST /reviews/ | 324.49 | 1113.81 | 61.60 | 18.22 |

The only thing to keep in mind is that it can’t do heavy computation similar to JS. But could make a hell of an HTTP server and has build-in support for

  • HTTP/1.1 requests streaming.
  • Websockets.
  • HTTP/2 server push.
  • HTTP/3 (I think it's already in work)

If you plan to migrate your old flask codebase to Quart it’s easy as pie, See the Code for yourself.

import asyncio

from quart import Quart

from routes.disk_api import disks_api


app = Quart(__name__)


#routes for disk api

app.register_blueprint(disks_api, url_prefix='/api/disks')


if __name__ == '__main__':
app.run(host = '0.0.0.0',port=5000)

So guys why not give it a trie, plus don't try to block the event loop 😝

Visit https://gitlab.com/pgjones/quart for more info

--

--