There are workarounds for Flask’s dependency injection shortcomings, I wrote a post about how to dependency_overrides = override_route53_dependency yield c app. In our app _init_.py file ( visit source), we define a db global:Īsync def override_route53_dependency () -> MagicMock : mock = MagicMock () return mock () def client () -> Generator : with TestClient ( app ) as c : app. The database in a user auth example: Typical Flask Approach to Dependencies Or hanging values on flask.g (which is just another global). With Flask, you will often find yourself exporting globals, This is an area where Flask is very weak. FastAPI allows you toĭo this at the level of path operation functions, i.e. Is a fancy way of saying your code has certain requirements to work. What I love the most about FastAPI is its dependency injection mechanism. This all results in faster development speed.ģ. Pydantic does the job and is super intuitive (there’s no new syntax to learn). The docs of Django REST Framework serializers. Python libraries like Marshmallow, or digging into In your other Python API projects you may have found yourself making use of serialization getenv ( 'SECRET_KEY', 'my_precious' ) DEBUG = False BCRYPT_LOG_ROUNDS = 13 SQLALCHEMY_TRACK_MODIFICATIONS = False class DevelopmentConfig ( BaseConfig ): """Development configuration.""" DEBUG = True BCRYPT_LOG_ROUNDS = 4 SQLALCHEMY_DATABASE_URI = postgres_local_base + database_name In FastAPI, a combination of Python type hints and Pydantic models define your endpointĮxpected data schemas (both inputs and outputs).Ĭlass BaseConfig : """Base configuration.""" SECRET_KEY = os. How FastAPI reduces your errors with type declarations and Pydantic schemas Web server - uvicorn is the recommended choice, although it’sĢ. You do need to use FastAPI together with an ASGI Workarounds) to implement using sync/WSGI. This leads to significant performance improvements.įurthermore, ASGI servers and frameworks also give you access to inherently concurrentįeatures (WebSockets, Server-Sent Events, HTTP/2) that are impossible (or at least require With FastAPI, your application will behave in a non-blocking way throughout the stack, concurrencyĪpplies at the request/response level. Servers, applications, middleware, and individual components. It enables interoperability within the whole Python async web stack: Within path operations, your Flask server will still tie up a worker for each request.įastAPI on the other hand implements the ASGI specification.ĪSGI is a standard interface positioned as a spiritual successor to WSGI. Versions of Flask (2.x) you can get a performance boost by making use of an event loop However, Flask is fundamentally constrained in that it is a WSGI application. (Both frameworks use decorators to mark ( '/' ) async def read_results (): results = await some_library () return results In this regard, Flask (as of v2.x) and FastAPI are identical. In practice, this means declaring coroutine functions with the async keyword, and using theĪwait keyword with any IO-bound parts of the code. Who also created the Django REST Framework). from a client browser)įastAPI is built on top of Starlette, an ASGIįramework created by Tom Christie (he is a Python community powerhouse IO-bound network code (which is most APIs), where you have to wait for something, for example: Which was added in Python 3.4 and allows you to write concurrent code. How is this possible in slow old Python? Under the hood, FastAPI is leveraging Python’s asyncio library, Naturally, benchmarks should be taken with a pinch of salt, have a look at the source of these The sort of high-performance you would expect from traditionally faster languages like NodeJS The FastAPI toolboxįastAPI’s name may lack subtlety, but it does what it says on the tin. FastAPI’s elegant dependency injection 4. How FastAPI reduces your errors with Python type declarations and Pydantic 3. I’m willing to concede that a better title for this post would be “why use FastAPI instead of Flask”. This post compares and discusses code from an example Flask and FastAPI project. This particular web framework that makes it worth switching away from your tried-and-tested Flask APIs? More and more people are getting onboard the FastAPI train.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |