Falcon

Unburdening APIs for over 0.00 x 10-2 centuries.

As featured in TalkPython Episode #129

Get started Contribute
# sample.py

import falcon

class QuoteResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        quote = {
            'quote': (
                "I've always been more interested in "
                "the future than in the past."
            ),
            'author': 'Grace Hopper'
        }

        resp.media = quote

api = falcon.API()
api.add_route('/quote', QuoteResource())
$ pip install falcon
$ gunicorn sample:api

Design

We designed Falcon to support the demanding needs of large-scale microservices and responsive app backends. Falcon complements more general Python web frameworks by providing bare-metal performance, reliability, and flexibility wherever you need it.

Fast. Same hardware, more requests. Falcon turns around requests several times faster than most other Python frameworks. For an extra speed boost, Falcon compiles itself with Cython when available, and also works well with PyPy. Considering a move to another programming language? Benchmark with Falcon + PyPy first.

Reliable. We go to great lengths to avoid introducing breaking changes, and when we do they are fully documented and only introduced (in the spirit of SemVer) with a major version increment. The code is rigorously tested with numerous inputs and we require 100% coverage at all times. Six and mimeparse are the only third-party dependencies.

Flexible. Falcon leaves a lot of decisions and implementation details to you, the API developer. This gives you a lot of freedom to customize and tune your implementation. Due to Falcon's minimalist design, Python community members are free to independently innovate on Falcon add-ons and complementary packages.

Debuggable. Falcon eschews magic. It's easy to tell which inputs lead to which outputs. Unhandled exceptions are never encapsulated or masked. Potentially surprising behaviors, such as automatic request body parsing, are well-documented and disabled by default. Finally, when it comes to the framework itself, we take care to keep logic paths simple and understandable. All this makes it easier to reason about the code and to debug edge cases in large-scale deployments.

Perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away.

Features

  • Highly-optimized, extensible code base
  • Intuitive routing via URI templates and REST-inspired resource classes
  • Easy access to headers and bodies through request and response classes
  • DRY request processing via middleware components and hooks
  • Idiomatic HTTP error responses
  • Straightforward exception handling
  • Snappy unit testing through WSGI helpers and mocks
  • CPython 2.6-2.7 and 3.3+, or PyPy 2.7 and 3.5+
  • Cython support for an extra speed boost under CPython

Don't lower your expectations to meet your performance. Raise your level of performance to meet your expectations. Expect the best of yourself, and then do what is necessary to make it a reality.

Benchmarks

To give you a feel for Falcon's performance, we've included the results of running a simple benchmark across several popular Python web frameworks. We work very hard to ensure that Falcon itself will not be the bottleneck in your application.

Scenario. The benchmark acts as a WSGI server and performs a GET request directly on each framework's PEP-3333 app. Each app parses a route template with a single embedded parameter, reads a query parameter and a header from the request data, sets an x-header on the response, and finally returns a 10 KiB plain-text body, randomly generated.

Method. 20,000 iterations were executed per trial, and the best time was recorded for each framework over 20 trials. The order in which the frameworks were tested was randomized for each trial. Also, garbage collection was enabled as it would be in a production environment, and a full collection was forcefully triggered before each trial. Finally, results were calculated using the Decimal class, and rounded to the nearest whole number.

Platform. Benchmarks were executed on a DigitalOcean 1GB VM, running Ubuntu 16.04.2 with all system packages updated to their latest versions as of May 2017. The frameworks were tested on several different versions of Python. Note that in these tests the Falcon modules were not cythonized; generally speaking, the reader may expect a ~10% boost in performance when Falcon is installed with Cython.

CPython 2.7.13

CPython 3.6.1

PyPy2 5.7.1

PyPy3 5.7.1-beta0

Extended Test

Falcon was also benchmarked under CPython 2.7.13 with a more realistic scenario, in which the routing table had multiple entries, the query string contained percent-encoded characters, and several complex response headers were set in the response. Note that even when Falcon is doing more work, it still is able to outperform other frameworks, such as Bottle, which are only doing the bare minimum to construct a response.

In order to be great, you just have to care. You have to care about your world, community, and equality.

Community

Falcon is an Apache-licensed community project, built and maintained by stylish volunteers from around the world.

PyCon 2016 Sprints

Kurt Griffiths (kgriffs on GH, Gitter, and Twitter) is the original creator of the Falcon framework, and currently co-maintains the project along with John Vrbanac (jmvrbanac on GH and Gitter, and jvrbanac on Twitter).

Check out our Falcon talks, podcasts, and blog posts wiki page to learn more about the project, and to add your own resources.

A number of Falcon add-ons, templates, and complementary packages are available for use in your projects. We've listed several of these on the Falcon wiki as a starting point, but you may also wish to search PyPI for additional resources.

The Falconry community on Gitter is a great place to ask questions and share your ideas. You can find us in falconry/user. We also have a falconry/dev room for discussing the design and development of the framework itself.

Per our Code of Conduct, we expect everyone who participates in community discussions to act professionally. Please lead by example in encouraging constructive discussions. Each individual in the community is responsible for creating a positive, constructive, and productive culture.