Python Async Web Framework

Perhaps one of the biggest leaps from Python 2 to Python 3 is the introduction of asynchronous programming. This is followed by the development of Python async web frameworks which take advantage of this breakthrough. While Python web frameworks are still being dominated by Flask and Django, many developers began to explore the benefits of using Python async web framework.

You can find dozens of Python async web frameworks that have been developed and ready to be downloaded. However, it would be burdensome if you try all async frameworks to discover which one is the fastest or the most suitable for your needs. Here are the seven most popular Python async web frameworks you can start exploring:
1. Sanic
2. Tornado 
3. Japronto 
4. Aiohttp 
5. Vibora
6. Quart 
7. FastAPI

Sanic 

sanic Python async web framework

Sanic is a Python 3.6+ framework that is designed for fast HTTP responses by using async/await syntax. It is a Flask-like framework and built on top of UVLoop, a Python wrapper for libuv.

It was reported that Sanic can handle 33k request per second. However, with the optimization of multiple processes, it is capable of handling 186k request/second (source). Sanic web framework would surely help you run a high performance HTTP server that you can simply expand, develop up to the maximum.

Sanic is undoubtedly the most popular Python async web framework on the market. It is the perfect stepping-stone framework if you want to move from ordinary frameworks to async web frameworks. It has syntax equivalent to other microframeworks, with a few async interposed on it.

More importantly, Sanic has all basic features that you need to build modern websites. These features include routing, cookies, sockets, middleware, versioning, static files, class-based views and more. When you need more features Sanic doesn’t support such as templating, file I/O, queues, database interfaces, you can always look at async libraries that are ready to be added.

Start learning Python from UNLIMITED Books, Videos and Tutorials!

Tornado 

tornado Python async web framework

Tornado is in fact a group of Python asynchronous modules. It consists of asynchronous servers and clients, Coroutines and Networking modules. These modules have been blended to make an async web framework.

Tornado web framework allows developers to scale web services over Python previously unattainable in a concurrent manner.

Tornado can be said as the pioneer of asynchronous framework in Python. It has been around since 2009, was developed by a particular company known as the Friend Feed. This means that Tornado has already been supporting asynchronous even Python hasn’t been facilitated it officially.

Tornado was designed to tackle C10k problem. The problem that arise due to the inability of server to scale beyond 10,000 concurrent connections. It takes advantage of the non-blocking web server and tools (like epoll or kqueue) to optimize the similar traditional web frameworks.

Another benefit of Tornado is that it sits between Flask and Django, two most popular Python web framework. When you have a project that doesn’t require a whole lot of tools and libraries like Django but doesn’t want to build it by using a bare bone like Flask, then Tornado is the right choice. It comes prepackaged with basic site building blocks, ensuring faster development time yet delivering a satisfactory result at the end.

More importantly, those prepackaged tools incorporate many of the web development's most complicated and time-consuming tasks. It includes user authentication, templates, localization, cookies, static file caching, third party authentication and many more. It is also relatively easy to integrate Tornado with other frameworks.

Japronto 

Japronto is a Python async web framework which is extremely fast, scalable, and lightweight. The name Japronto comes from a Portuguese word meaning already done. Similar to Sanic, Japronto was built based on UVLoop.

Japronto proclaimed that it has the ability to handle over 1.2 million requests per second, compared to 54k requests for Node.js and 34k requests for Sanic.

The secret behind this remarkable performance lies in its optimization of HTTP pipelining. It doesn’t require a response to the client before other requests are sent. This means that once Japronto receive data, parses a number of requests from the data, it tries to return the responses based on the order of incoming requests swiftly. Finally, all the responses are tied together into a single system call.
Japronto uses PicoHTTPParser for parsing status line, headers, and HTTP message body. PicoHTTPParser is a library written entirely in C. All core components of Japronto are also written in C. A broad range of optimizations are applied to tweak performance.

Japronto is designed for exceptional projects where every nanosecond counts. Experience developers who passionate about optimization will obtain all of its advantages.

Furthermore, Japronto is intended to deliver a robust microservice framework using REST APIs. On that account, Japronto doesn’t carry much in the box (a barebone framework).

In addition, you must take into consideration that you can determine which routes should be handled synchronously or asynchronously. This is essential because some requests that can use synchronous handlers, shouldn’t be treated asynchronously, or else the overhead of switching between coroutines will slow the performance down.

One thing to consider is that this framework is one of the most recent frameworks that came into the market. It needs more development and implementations to prove its robustness.

Aiohttp 

aiohttp Python async web framework

Aiohttp is a library based on Python asyncio that provide so many things related to HTTP. Accordingly, aiohttp is not only powerful toolkit to build server applications, but also handy to develop client apps.

In spite of aiohttp is more like a toolbox than a full web framework, you can get all the essential features of a microframework. That include routing, middleware and signals. If you need more functionalities, than you can easily grab what you need from asyncio-compatible libraries.

By default, you can count on the performance of aiohttp. However, you may add extra libraries such as cchardet, aiodns and others. You can even make use of uvloop, the package that powers Sanic web framework.

More importantly, testing is a substantial part within aiohttp design. It is remarkably easy to test your aiohttp application, particularly by using pytest.  Aiohttp is also actively being maintained and developed, which makes this framework a very good choice.

Vibora 

vibora Python async web framework

Vibora is another async microframework which was strongly influenced by Flask and Sanic. Nevertheless, many features such as sessions, template engine, schemas validation and  others were written from scratch to optimize the performance and provide a stylish async interface.

Furthermore, Vibora incorporates famous libraries like jinja2, werkzeug, websockets by aaugustin, marshmallow and many more into its builtin package. Vibora comes with a good documentation that will ease you exploring this framework.

Currently Vibora is under heavy development. While it is a good sign that this framework is well maintained and developed, but for anyone who grab vibora earlier may soon perform a large scale upgrade.

Enroll Nanodegree in Programming with Python Now!

Quart 

quart Python async web framework

You may have noticed that there have been a lot of new Python web frameworks released. The new frameworks mostly took advantage of Python’s async feature. Nevertheless, these new frameworks came out with their own new APIs. This would require time and effort to learn the new system and migrating to them.

If you are an experience flask developer and wishing to have a smooth transition to async web framework, you’ll find Quart a perfect framework for you. The creator of Quart tried to make this framework 100% compatible with Flask, and just put async, HTTP 2 and WebSockets support to it.

This means that you can learn Quart from Flask documentation, just be aware that Quart’s functions are asynchronous. What is more, you can migrate Flask apps into Quart apps with very little effort and then you’ll get performance improvement couldn’t be attained with Flask (source). Undoubtedly, if you are serious about making a transition to Quart, then you have to use Python 3.7 or higher.

Other advantages of Quart are that all Flask’s features are accessible: routing, sessions, blueprints, middleware, templating and many more. More importantly, you can use Flask extensions from Quart. Quart is also compliant with Asynchronous Server Gateway Interface (ASGI), an interface between web servers and web apps so they can forward requests asynchronously.

FastAPI

fastapi Python async web framework

According to its documentation, FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. However, it's certainly not an API only framework. You can definitely build web apps with this web framework.

The author of this framework stated that he tried not to create another framework. Nevertheless, when most frameworks, plugins and tools could not provide all the features that he needed then the only option is creating a new framework.

It is a clear indication that FastAPI was built based on extensive examination of many other frameworks. Aside from Python frameworks like Django, Flask, and Sanic, the author also looked at framework from another language such as NestJS (source).

The result is not only a feature rich framework but also a pleasant and enjoyable syntax compared to other frameworks. The python type hint makes editors and tools provide support like type checks and autocompletion. Combined with dependency injection system that would minimize code repetition, create a less bugs prone code and lead to good coding principles.

Moreover, FastAPI automatically generates API documentation. You can use Swagger UI, ReDoc or other user interfaces to OpenAPI to generate API doc. Automatic data model documentation is also supported by using JSON Schema.

Other remarkable features that make FastAPI a lot better than other frameworks include:

  • FastAPI is independent of database or ORM. FastAPI support any database system including SQL and NoSQL.
  • It has integrated support for OAuth 2.0 that makes integration with external OAuth 2.0 providers so easy. 
  • It has support for WebSockets, GraphQL, CORS, sessions and many others.
  • FastApi is built based on Starlette (using ASGI) and Pydantic which make its performance surpass Node.js and even GO.
  • It provides extensive documentation, on a par with Django docs.

Despite its astonishing features, it has relatively small community because FastAPI is a new framework. Nevertheless, some enthusiasts believe that FastAPI is the next best thing in Python web frameworks.

Conclusion

Python Async 
Frameworks
Main Feature
Sanic The most popular
Tornado The pioneer
Japronto The fastest
Aiohttp Server and client
Vibora Flask and Sanic amelioration
Quart The easiest to migrate
FastAPI The most feature-rich

The introduction of asynchronous programming to Python has led to the emergence of new async web frameworks. There are different options you can select if you want to start venturing Python async web frameworks. Whether it is the most popular, the easiest to switch, the fastest one or the most feature rich, you have a wide range of choices that suit your needs.

Just bear in mind that most of the frameworks are in early stage of development and have relatively small community. Some of them are being rapidly evolve and need extra caution if you want to adapt them. Always recheck, particularly to the community, will there be a major upgrade to the framework or not if you want to use it to a production level project.


0 Comments