FastAPI vs Flask - The Complete Guide
Introduction More and more people are getting onboard the FastAPI train. Why is this? And what is it about this particular web framework that makes it worth switching away from your tried-and-tested Flask APIs? This post compares and discusses code from an example Flask and FastAPI project. The sample project is a JSON web token (JWT) auth API. Here is the full source code . I’m willing to concede that a better title for this post would be “why use FastAPI instead of Flask”. Contents 1. FastAPI’s Performance FastAPI’s name may lack subtlety, but it does what it says on the tin. With FastAPI, you get the sort of high-performance you would expect from traditionally faster languages like NodeJS or Go. Naturally, benchmarks should be taken with a pinch of salt, have a look at the source of these How is this possible in slow old Python? Under the hood, FastAPI is leveraging Python’s asyncio library , which was added in Python 3.4 and allows you to write concurrent code. Asyncio is a great fit for IO-bound network code (which is most APIs), where you have to wait for something, for example: Fetching data from other APIs Receiving data over a network (e.g. from a client browser) Querying a database Reading the contents of a file FastAPI is built on top of Starlette , an ASGI framework created by Tom Christie (he is a Python community powerhouse who also created the Django REST Framework ). In practice, this means declaring coroutine functions with the async keyword, and using the await keyword with any IO-bound parts of the code. In this regard, Flask (as of v2.x) and FastAPI are identical. (Both frameworks use decorators to mark endpoints): Flask: @app.route ( "/get-data" ) async def get_data (): data = await async_db_query ( ... ) return jsonify ( data ) FastAPI: @app.get ( '/' ) async def read_results (): results = await some_library () return results However, Flask is fundamentally constrained in that it is a WSGI application. So whilst in newer versions of Flask (2.x) you can get a performance boost by making use of an event loop within path operations, your Flask server will still tie up a worker for each request. FastAPI on the other hand implements the ASGI specification. ASGI is a standard interface positioned as a spiritual successor to WSGI. It enables interoperability within the whole Python async web stack: servers, applications, middleware, and individual components. See the awesome-asgi github repo for some of these resources. With FastAPI, your application will behave in a non-blocking way throughout the stack, concurrency applies at the request/response level. This leads to significant performance improvements. Furthermore, ASGI servers and frameworks also give you access to inherently concurrent features (WebSockets, Server-Sent Events, HTTP/2) that are impossible (or at least require workarounds) to implement using sync/WSGI. You do need to use FastAPI together with an ASGI web server - uvicorn is the recommended choice, although
https://christophergs.com/python/2021/06/16/python-flask-fastapi/
0 コメント:
コメントを投稿