Python Falcon - WSGI vs ASGI
Last Updated :
24 Apr, 2025
When developing web applications or APIs with Falcon, one crucial decision you'll need to make is whether to use WSGI (Web Server Gateway Interface) or ASGI (Asynchronous Server Gateway Interface) as the communication protocol between Falcon and your web server. In this article, we'll explore the differences between WSGI and ASGI and help you decide which one is the right choice for your Falcon application.
What is Python Falcon
Python Falcon is a lightweight web framework in Python. It is used for building high-performance APIs (Application Programming Interfaces). It is a minimalist ASGI/WSGI framework for building REST APIs with a focus on performance and reliability. In this article, we will look at the difference between WSGI vs ASGI
Some key features of Python Falcon:
- Performance-oriented - Used in projects where speed and low latency are crucial, such as microservices and high-traffic APIs.
- Minimalistic - This framework only provides the essential tools and features for building APIs. Not more than that.
- HTTP-centric - Falcon is builtMinimalistic around HTTP protocols which makes developers think in terms of HTTP requests (GET, POST, DELETE, etc.).
- Middleware support - This allows us to add custom processing to requests and responses.
- WebSocket Support - This makes Falconis built suitable for applications requiring real-time and bidirectional communications.
- ASGI / WSGI - Falcon supports both ASGI (Asynchronous Server Gateway Interface) and WSGI (Web Server Gateway Interface).
- Well-documented - Falcon has comprehensive documentation and a Falconsupportive community.
When we have to handle HTTP requests and responses in Falcon, there are two different protocols -
- WSGI - Web Server Gateway Interface
- ASGI - Asynchronous Server Gateway Interface
WSGI (Web Server gate Interface)
WSGI is synchronous, which means it handles one request at a time. When a request comes in, it's processed by the web server and the WSGI application sequentially. This makes it suitable for applications with low to moderate traffic.
Some key features of WSGI
- Synchronous Processing
- WSGI is a synchronous protocol, which means that it handles one request at a time.
- Each incoming HTTP request is processed sequentially. In the meantime, it blocks the server until the processing is completed.
- This limits the responsiveness and concurrency of the application, especially when we are dealing with long-running tasks.
- Compatibility
- WSGI is a traditional, and widely accepted interface for Python applications.
- It is compatible with many web servers and hosting environments.
- Simplicity
- Writing code is pretty Straightforward and simple,
- It is a good choice for small to medium APIs which can bring moderate size traffic.
Example
Python3
import falcon
# Create a Falcon application
app = falcon.App()
# Define a resource to handle requests
class HelloWorldResource:
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
resp.body = "Hello, WSGI World!"
# Add the resource to the application
app.add_route("/", HelloWorldResource())
# Run the application using a WSGI server (e.g., gunicorn)
# Install gunicorn with: pip install gunicorn
# Run with: gunicorn myapp:app
# OR
# Install waitress with: pip install waitress
# Run with: waitress-serve --host=0.0.0.0 --port=8000 filename:app
Code Explanation
We created a Flask WSGI application using "flask" module. This is a basic example just showing difference between WSGI and ASGI working.
- Import the library using command
- import flask.
- Before that run command pip install flask in your terminal to install flask.
- Create a falcon app
- app = Flask(__name__)
- This app object will be used to define routes and handle HTTP requests.
- Defining a falcon resource class
- class HelloWorldResouce is a resource class.
- In python, resources are Python classes that handle http requests(GET POST PUT)
- Here, "on_get" method is defined to handle GET requests.
- When a GET request is received, it sets the response status to 200 (ok) and shows the response body with output "Hello WSGI World!" at localhost:8000.
- Now, add this resource to your application:
- app.add_route("/", HelloWorldResource())
- This line executes the HelloWorldResource at root URL "/".
- When the GET request is made to the root URL "/", Falcon will invoke on_get method of the HelloWorldResource class.
- Final step is to run the application using some WSGI server.
- Gunicorn - pip install gunicorn
- Run the file and then type this command in your terminal
- waitress - pip install waitress
- Run the file and then type this command in your terminal
- waitress-serve --host=0.0.0.0 --port=8000 filename:app
- Your can choose any other port (here 8000)
- Output of above code
- The output of the above code will be visible at localhost:8000 or at port you chose.
- This is the output screen.
Output
.png)
ASGI (Asynchronous Server Gateway Interface)
ASGI, or Asynchronous Server Gateway Interface, is a more recent standard that's designed to handle asynchronous web applications and real-time communication more efficiently. It allows Falcon and other Python web frameworks to work with asynchronous web servers and take advantage of asynchronous programming techniques.
- Synchronous Processing
- ASGI is an asynchronous protocol, which means that it allows parallel processing of multiple requests.
- It handles real-time, long-polling and Websocket connections efficiently.
- Compatibility
- ASGI is newer than WSGI.
- This may require web servers and hosting environments that support ASGI, like Daphne, hypercorn or uvicorn.
- Complex Use Cases
- ASGI is ideal for complex and high traffic applications.
- It is ideal for application processing in real time like chat applications, streaming services and live notifications.
Example
Python3
import falcon
app = falcon.App()
class HelloWorldResource:
async def on_get(self, req, resp):
resp.status = falcon.HTTP_200
resp.text = "Hello, ASGI World!"
app.add_route("/", HelloWorldResource())
Code Explanation
- Import the library using command
- Falcon is used to create ASGI application and uvicorn is used to run the application.
- Create a falcon app
- app = falcon.app()
- This app object will be used to define routes and handle HTTP requests.
- Similar to WSGI code, we define a falcon resource class
- class HelloWorldResouce is a resource class.
- Resources are Python classes that handle http requests(GET, POST, PUT)
- Here, "on_get" method is defined to handle GET requests.
- When a GET request is received, it sets the response status to 200 (ok) and shows the response body with output "Hello ASGI World!" at localhost:8000.
- Again similar to previous code, add this resource to your application:
- app.add_route("/", HelloWorldResource())
- This line executes the HelloWorldResource at root URL "/".
- When the GET request is made to the root URL "/", Falcon will invoke on_get method of the HelloWorldResource class.
- Finally, run the application using some ASGI server.
- Run the file and then type this command in your terminal
- Run the file and then type this command in your terminal
- pip install waitress
- waitress-serve --host=0.0.0.0 --port=8000 filename:app
- Your can choose any other port (here 8000)
- Output of above code
- The output of the above code will be visible at localhost:8000 or at port you chose.
- This is the output screen.
Output
Output of ASGI app at localhost:8000
How to choose between ASGI and WSGI with Falcon?
The choice depends on specific requirements of the application. For example , if we have a simple API with low to moderate traffic, and we want wide range compatibility, we can opt for WSGI. If we have a complex API with high traffic and application uses real-time features like Websocket, we must opt for ASGI. This way, we can handle simultaneous connections efficiently.
Difference Table WSGI vs ASGI
|
Full form
| Web Server Gateway Interface
| Asynchronous Server Gateway Interface
|
Synchronous Processing
| Handles one request at a time
| Allows parallel processing of multiple requests at a time
|
Blocking Behavior
| Blocks the server until the processing of one request is completed.
| Does not block the server
|
Compatibility
| Traditional, so runs on mojority web servers and hosting environments
| Newer protocol, so may require specific web servers and hosting environments
|
Usage
| Used for simple and straightforward applications that use simple APIs with low to moderate traffic.
| Used for complex and high traffic applications.
|
Example servers/hosts
| waitress, gunicorn
| waitress, uvicorn
|
Similar Reads
Python Falcon - API Testing
Python Falcon is a lightweight and fast web framework designed for building RESTful APIs. When it comes to API testing, Falcon provides a straightforward and efficient way to interact with your API endpoints. In this article, we'll explore three simple examples using Python Falcon: a basic API endpo
2 min read
Build APIs with Falcon in Python
In the sector of web development, building a sturdy and green API is vital. APIs (Application Programming Interfaces) act as a bridge between software program structures, allowing for easy verbal exchange and record change. Python, with its flexibility and flexibility, is a popular choice for growin
6 min read
Python Falcon - Uvicorn
Python-based libraries, Falcon and Uvicorn are two powerful tools that, when used together, form a robust framework for building high-performance web applications. Falcon is a minimalist web framework designed for building fast and efficient APIs. Uvicorn serves as an ASGI server, bringing asynchron
3 min read
Python Falcon - Waitress
In this article, we will explore Python Falcon-Waitress, a powerful combination for building web APIs with Falcon, and we'll illustrate its usage with practical examples. Falcon-Waitress offers seamless integration of the Falcon framework with the Waitress production-quality WSGI server, enabling yo
7 min read
Python Falcon - App Class
Python Falcon App class serves as the foundation for creating scalable and efficient web applications. In this article, we'll discuss the Falcon's App class, exploring its features, and benefits. What is Python Falcon - App Class?Falcon App class is essential for any Falcon-based application. It pro
4 min read
Python Falcon - Websocket
Websockets provide a powerful mechanism for establishing bidirectional communication between a client and a server over a single, long-lived connection. Python Falcon, a lightweight and fast web framework, offers support for implementing websockets, enabling real-time communication between clients a
3 min read
Python Falcon - Routing
Falcon is Python based a lightweight and high-performance Python-based web framework designed for building fast and efficient APIs. There are also other Python-based frameworks for building APIs such as Flask, FastAPI, etc. Routing is a fundamental concept in Falcon, as it determines how incoming HT
4 min read
Python Falcon - Cookies
In the world of web development, managing user sessions is a fundamental aspect, and cookies serve as a crucial tool in achieving this. Python Falcon, a lightweight and efficient web framework, allows developers to seamlessly handle cookies within their applications. In this article, we will learn a
5 min read
Python for AI
Python has become the go-to programming language for artificial intelligence (AI) development due to its simplicity and the powerful suite of libraries it offers. Its syntax is straightforward and closely resembles human language, which reduces the learning curve for developers and enables them to f
7 min read
Python Falcon - Request & Response
Python Falcon, a nimble web framework, simplifies the process of building web APIs by efficiently managing incoming requests, which are messages asking for something from your API, and providing an easy way to create structured responses that Falcon then sends back to the requester, making it a swif
7 min read