- Python Falcon - Discussion
- Python Falcon - Useful Resources
- Python Falcon - Quick Guide
- Python Falcon - Deployment
- Python Falcon - Testing
- Python Falcon - Sqlalchemy Models
- Python Falcon - Websocket
- Python Falcon - CORS
- Python Falcon - Middleware
- Python Falcon - Hooks
- Python Falcon - Error Handling
- Python Falcon - Status Codes
- Python Falcon - Cookies
- Python Falcon - Jinja2 Template
- Python Falcon - Inspect Module
- Falcon - Suffixed Responders
- Python Falcon - Routing
- Python Falcon - App Class
- Python Falcon - Resource Class
- Request & Response
- Python Falcon - API Testing Tools
- Python Falcon - Uvicorn
- Python Falcon - ASGI
- Python Falcon - Waitress
- Python Falcon - Hello World(WSGI)
- Python Falcon - WSGI vs ASGI
- Python Falcon - Environment Setup
- Python Falcon - Introduction
- Python Falcon - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Falcon - ASGI
ASGI stands for Asynchronous Server Gateway Interface (as per its official documentation, it is a spiritual successor to WSGI), it adds the async capabipties to Python web servers, apppcations and frameworks.
For running an async web apppcation, we ll need an ASGI apppcation server. Popular choices include −
Uvicorn
Daphne
Hypercorn
We shall use Uvicorn server for async examples in this tutorial.
Hello World - ASGI
The ASGI related functionapty of Falcon is available in the falcon.asgi module. Hence, we need to import it in the beginning.
import falcon import falcon.asgi
While the resource class remains the same as in the previous example, the on_get() method must be declared with async keyword. we have to obtain the instance of Falson s ASGI app.
app = falcon.asgi.App()
Example
Hence, the hellofalcon.py for ASGI will be as follows −
import falcon import falcon.asgi class HelloResource: async def on_get(self, req, resp): """Handles GET requests""" resp.status = falcon.HTTP_200 resp.content_type = falcon.MEDIA_TEXT resp.text = ( Hello World ) app = falcon.asgi.App() hello = HelloResource() app.add_route( /hello , hello)
To run the apppcation, start the Uvicorn server from the command pne as follows −
uvicorn hellofalcon:app –reload
Output
Open the browser and visit http://localhost:/8000/hello. You will see the response in the browser window.
Advertisements