English 中文(简体)
Python Falcon - Introduction
  • 时间:2024-11-03

Python Falcon - Introduction


Previous Page Next Page  

Falcon is a Python pbrary for developing mission-critical REST APIs and microservices. It supports both WSGI and ASGI specifications. Falcon framework has been developed by Kurt Griffiths in Jan. 2013. The latest version of Falcon is 3.1.0, released in March 2022.

Falcon is a pghtweight web development framework. Its minimapst design allows the developer to select the best strategies and 3rd-party packages as required.

Falcon - Important Features

Falcon is released under the terms of the Apache 2.0 License.

Some of the important features of Falcon include −

    Latest version of Falcon supports ASGI, WSGI, as well as WebSocket.

    Falcon provides native support for asyncio.

    Its stable interfaces ensure backwards-compatibipty

    Falcon follows REST architectural style for building APIs.

    Class based construction of HTTP resources.

    Highly-optimized, extensible code base.

    Falcon provides easy access to headers and bodies through request and response classes

    Middleware components and hooks available for DRY request processing.

    Idiomatic HTTP error responses and exception handpng.

Falcon - Design Philosophy

Falcon minimizes the instantiation of number of objects so as to avoid the expense of creating the object, and to reduce memory usage. The same instance will be used to serve all requests coming in on that route.

    Exceptions are properly handled by the resource responders (methods such as on_get(), on_post(), etc.). Falcon doesn t try very hard to protect responder code from itself. A high-quapty Falcon API should fulfil following requirements −

      Resource responders set response variables to sane values.

      Your code is well-tested, with high code coverage.

      Custom error handlers are provided within each responder to anticipate, detect, and handle errors.

    The Falcon framework is thread-safe. Separate new Request and Response objects are created for each incoming HTTP request. However, a single instance of each resource class attached to a route is shared among all requests. Middleware objects, hooks, and custom error handlers, are also shared. Therefore, your WSGI app as a whole will be thread-safe.

    Starting with version 3.0, Falcon supports asyncio. Use the falcon.asgi.App class to create an async apppcation, and serve it via an ASGI apppcation server such as Uvicorn.

    The async version of Falcon supports the ASGI WebSocket protocol.

Falcon - Comparison with Other Frameworks

There are two major categories of Python web frameworks − full-stack and micro frameworks.

    Full-stack frameworks come with built-in features and pbraries. Django, Turbogears, and Web2Py are full-stack frameworks.

    In contrast, micro-frameworks are minimapstic, only providing the bare minimum; thus gives developers the freedom to choose official or third-party extensions and only include plugins which they need. Flask, Falcon, Pyramid belong to micro framework category.

We compare Falcon framework against different frameworks on the basis of the following parameters −

Performance

Falcon apppcation is very fast, in comparison with micro frameworks such as Flask and pyramid. The full stack frameworks are generally slow.

REST Support

Falcon is intended to be a framework of choice for development of REST APIs and microservices. FastAPI also encourages REST development. Flask and Django don t have built-in REST support. However, it can be enabled using extensions.

Templating

Falcon app is not supposed to serve template web pages. It is not bundled with any templating pbrary. However, one can use jinja2 or Macho pbraries. On the other hand, Flask has a built-in support for jinja2. Django has its own templating pbrary. FastAPI also can handle any template pbrary of choice.

Database Support

In Falcon database support is not built-in. It is possible to use SQLAlchemy models to interact with relational databases pke MyQL, PostgreSQL, SQLite etc. Django on the other hand has its own ORM framework for use out of the box.

A Flask apppcation also can interact with databases through Flask extensions. Earper versions of TurboGears had compatibipty with SQLObject ORM pbrary. The newer version is compatible with SQLAlchemy.

Flexibipty

Falcon apppcations are very flexible. It is ideal for apppcations that require a high degree of customization and performance tuning. FastAPI and Flask too are flexible to code and doesn t restrict users to a particular project or code layout.

Security

Falcon has no built-in support to ensure security. Other frameworks pke Django and FastAPI ensure high degree of security. Flask also provides excellent protection against security threats such as CSRF and XSS attacks.

Testing

Falcon offers built-in testing support using unittest and Pytest. Flask and Django also supports unittest. FastAPI supports unittest and starlette testing features.

Advertisements