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.

  1. 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.
  2. Compatibility
    • ASGI is newer than WSGI.
    • This may require web servers and hosting environments that support ASGI, like Daphne, hypercorn or uvicorn.
  3. 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

  1. Import the library using command
    • Falcon is used to create ASGI application and uvicorn is used to run the application.
  2. Create a falcon app
    • app = falcon.app()
    • This app object will be used to define routes and handle HTTP requests.
  3. 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.
  4. 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.
  5. 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
      • pip install waitress
      • waitress-serve –host=0.0.0.0 –port=8000 filename:app
      • Your can choose any other port (here 8000)
  6. 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.

Python Falcon – WSGI vs ASGI

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.

Similar Reads

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...

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....

ASGI (Asynchronous Server Gateway Interface)

...

Difference Table WSGI vs ASGI

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....