WHAT IS SANIC?
Sanic is an asynchronous web framework and web server for Python 3.5+ that’s written to go fast. Sanic was developed at MagicStack and is based on their uvloop event loop, which is a replacement for Python asyncio’s default event loop, thereby making Sanic blazing fast. Syntactically Sanic resembles Flask.
Sanic maybe used as a replacement for Django or Flask to build highly scalable, efficient and blazing fast performant web applications.
It is preferable to use virtual environments in Python to create isolated environments with project-specific dependencies. Since Sanic 19.6+ versions do not support Python 3.5, we will work with Python 3.6+.
To install sanic in our Python virtual environment we will execute the following command – pip3 install sanic
Let us create a directory named sanic_demo & within it a file named main.py with the following lines of code –
We may either run main.py from an IDE, or run the file from Terminal by executing the following command – python3 main.py
The Sanic web server is up on 8000 port of our ‘localhost’.
Navigating to http://0.0.0.0:8000/ from our web browser renders “Hello World!”.
The config attribute of the Sanic app object is used to configure parameters. The app config object can be assigned key-value pairs as follows :
The full list of configuration params is available at the official documentation page – Sanic Config
ROUTING AND BLUEPRINTS
Sanic supports the route decorator to map handler functions to HTTP requests. We can use an optional parameter called methods in the ‘route’ decorator to work with any of the HTTP methods in the list.
Blueprints is a concept used for plugging sub-routes into the Sanic app from sub-modules of a large application. Blueprints must be registered into the Sanic app object. Using blueprints also avoids passing around the Sanic app object all over the application.
Let us modify our original main.py file to demonstrate the usage of routes and blueprints –
Let us create a new file named controller.py to declare our blueprints –
Let us run main.py and check the results when /my_bp endpoint is accessed –
We have used a web client called ‘Insomnia‘ to demonstrate our POST request –
Sanic routes can serve html files, json content, media files etc. To serve static content like images, pdf, static html files etc we need to use app.static() method which maps the path of a static file to an endpoint specified by a ‘route’.
Let us modify our main.py file to demonstrate this –
Running main.py and accessing http://0.0.0.0:8000/floral_image.jpg renders the image on the browser.
Let us further modify main.py to access some html content –
Let us create a sample index.html file –
Running main.py and accessing http://0.0.0.0:8000/display renders the following on the browser –
Exceptions can be explicitly raised within Sanic request handlers. The Exceptions take a message as the first argument and can also include a status code. The @app.exception decorator can be used to handle Sanic Exceptions. Let us demonstrate by tweaking our main.py file –
Exception rendered on browser –
NotFound (thrown when when request handler not found for route) and ServerError(thrown due to serve code issues) are most commonly used.
Web applications characteristically talk to external resources, like databases, queues, external APIs etc to retrieve information required to process requests. Sanic, a Python Web Framework that has Python 3.5+’s asyncio library’s async/await syntax pre-baked into it, is the ideal candidate for designing large scale I/O bound projects which work with many connections. This enables the webapp’s requests to be processed in a non-blocking and concurrent way.
Python 3.5 introduced asyncio, which is a library to write concurrent code using the async/await syntax (source: https://docs.python.org/3/library/asyncio.html). The asyncio library provides an event loop which runs async I/O functions. Sanic provides support for async/await syntax, thereby making request handling non-blocking and super fast. Adding the async keyword to request handler functions makes the function handle the code asynchronously, thereby leveraging Sanic’s performance benefits.
Instead of asyncio’s event loop, Sanic uses MagicStack’s proprietary uvloop which is faster than asyncio’s event loop, leading to blazing fast speeds. However, on Windows OS, Sanic reverts to asyncio’s event loop under the hood, due to issues with uvloop on Windows.
Reference: Sanic Official Docs.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Introduction to Kivy ; A Cross-platform Python Framework
- Introduction to the Spring Data Framework
- Python | Sessions framework using django
- Kivy - Python Framework for Mobile App development
- Python | Introduction to Web development using Flask
- Implement Token Authentication using Django REST Framework
- Django REST Framework Installation
- How to Create a basic API using Django Rest Framework ?
- Boolean Fields in Serializers - Django REST Framework
- String Fields in Serializers - Django REST Framework
- Core arguments in serializer fields - Django REST Framework
- Creating and Using Serializers - Django REST Framework
- URL fields in serializers - Django REST Framework
- File upload Fields in Serializers - Django REST Framework
- ListField in serializers - Django REST Framework
- IPAddressField in serializers - Django REST Framework
- Numeric fields in serializers - Django REST Framework
- Date and time fields in serializers - Django REST Framework
- Choice Selection Fields in serializers - Django REST Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.