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
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
gunicorn filename:app
waitress - pip install waitress
Run the file and then type this command in your terminal
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
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
uvicorn filename:app
Run the file and then type this command in your terminal
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
Characteristic
WSGI
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.