We earn commission when you buy through affiliate links.
This does not influence our reviews or recommendations.Learn more.
Asynchronous programming is a first-class citizen in Python now.
If youre a web developer, there are amazingframeworksyou can choose from!
As of writing, asynchronous is no more just a buzzword in the Python community.
Lets survey the current Python landscape and check out some of the top asynchronous frameworks.
Tornado
Surprisingly,Tornadoisnt a new framework at all.
Tornado isnt a web framework fundamentally.
Its a collection of asynchronous modules, which are also used to build the web framework module.
Of course, benchmarks are to be taken with a grain of salt.
If youre a performance junkie, though, Vibora might float your boat.
Quart is compliant with theASGIstandard, which is a successor to the famous WSGI standard and offers async support.
Feels (almost) exactly like Flask, doesnt it?!
In fact, you’re able to even use Flask extensions directly inside Quart.
FastAPI
The last (but most impressive) framework on this list isFastAPI.
Their development philosophy and extensive comparisons can be readhere.
SwaggerUI, ReDoc, and others are supported.
The framework also does automatic data model documentation with JSON Schema.
Extensive documentation:I dont know about you, but Im a total sucker for good documentation.
And in this area, FastAPI wins hands-down.
It has pages upon pages of docs explaining almost every little subtlety and watch out!
moments for developers of all levels.
And what about the performance?
BlackSheep
BlackSheepcan be used to create server-side or full-stack applications with an MVC pattern.
Quickly trigger the below commands one by one to set up the project.
Learn how to install PIP package installer on CentOS, Ubuntu and Windows.
We are done with setting up the project.
Lets create a file calledserver.pyand place the following code.
We have created our most famous hello world system.
There is only one route withHTTP GETmethod for now, and its /.
The functionhomeis called request handler in BlackSheep.
We have used arouterdecorator from the app.
There is another way to create routes i.e.., route.
We will be using therouterin this tutorial.
you could find thedocs.
Lets launch the program with the following command.
Go to thehttp://localhost:8000/in the web app.
You will see hello world in the online window.
Lets talk a bit about the command that we used to fire off the utility.
JSON Response
In the real world, we need the API responses in JSON in most cases.
We can return a JSON response from the method by wrapping the JSON object withjsonfrom theblacksheeppackage.
Lets see how we can do it.
We have importedjsonfrom theblacksheepand wrapped the JSON object with it.
Check it in the online window for JSON response.
Route Parameters
We need to accept the route params sometimes for the requests.
We can do it in BlackSheep by defining them inside the HTTP method.
We are accepting one route parameter calledname.
Go to thehttp://localhost:8000/Geekflare.
The method will return a greeting with the name given in the route.
The router decorator will pass the parameter with the same name to thehomefunction as its given to the decorator.
Here, it will bename.
If you change it in the decorator, change it in thehomefunction as well.
We can accept as many route parameters as possible in a similar way.
Lets see a quick example.
We have accepted one more route parameter calledinfo.
Go to http://localhost:8000/Geekflare/Chandan to check it.
Query Parameters
We dont need to do anything to accept the query parameters.
BlackSheep will automatically send the query parameters to the function in the form of a list.
Lets see the example.
Go tohttp://localhost:8000/?name=Geekflareto check the response.
Go tohttp://localhost:8000/?name=Geekflare&name=Chandanand check the output in the terminal.
You will see a list withGeekflareandChadanas we have passed two query parameters with the same name.
If you want multiple query parameters with different, you might do it too.
Just add another argument to the function with the query parameter name and do what you want with it.
Request Object
The only thing left in our basics is checking other HTTP methods.
Before going into it, lets check therequestobject for the API.
you’re able to see the following output in the terminal.
We can access different things from the request.
you’ve got the option to check the docs for it.
Here, our focus is on the request body.
Lets see how to get into the request body from the request object.
There is a method calledjsonin the request, which will return the data thats coming from the request.
Pass some data in the API request and call it.
You will see the data printing in the terminal that you have passed to the API.
HTTP Methods
We have seen the GET and POST methods in the above examples.
Similarly, you’re able to use the PUT, DELETE, etc.., methods as well.
Trying yourself wont be a problem as they are straightforward.
AIOHTTP
aiohttpis another framework that comes with the following key features.
Lets create a basic server-side system with aiohttp.
Quickly initiate the following commands to set up the project.
Create a file calledserver.pyand place the following code in it.
Theweb.Applicationinstance is our main app.
We have added the HTTP GET method with the route/which returns our favorite hello world.
The web.run_app function is used to execute the software, which takes theweb.Applicationinstance as an argument.
The functionhomeis called a request handler in aiohttp.
initiate the app with the following command.
Its the same as running normal python programs.
Go tohttp://localhost:8080/in the online window.
You will see hello world in the online window.
We can return the response in the JSON format usingweb.json_responsefunction.
Pass the JSON data to that function while returning the response.
Lets see the example.
If you go tohttp://localhost:8080/you will see the JSON object in the internet tool.
We can define the route parameters while adding the routes.
And they can be accessed from therequestargument of the request handler.
Lets see an example.
All route parameters can be accessed from therequest.match_infoas shown in the above example.
Go tohttp://localhost:8080/Geekflareto check it.
We can also have regex to match the routes.
We have added regex to the path parameter, which will accept only if the regex is passed.
Now, go tohttp://localhost:8080/1234567890you will see the response in the online window.
There is no need to add anything to accept query parameters.
We can accept the query parameters from therequest.queryobject.
Lets see an example.
Go tohttp://localhost:8080/?name=Geekflareand check the result.
You will seeGeekflarein the response that we are accessing from therequest.query.
We can pass multiple query parameters as well that can be accessed with their key names.
We have seen how to create a HTTP GET method in the above examples.
We need to know how to get into the request data before moving ahead.
Lets see an example of it.
In the above example, we have changed the API method from GET to POST.
We have accessed the request data usingrequest.jsonmethod.
Make a post request tohttp://localhost:8080/, you will see the request data printing in the terminal.
Similarly, you’re free to use the PUT, DELETE, etc.., methods as well.
Try them yourself and have fun.
you could continue exploring more about the framework in theirdocs.
Falcon
Falconis an ASGI framework to build REST APIs and microservices.
It has the following key features.
Lets set up a project to learn the basics of the falcon framework.
Set up the project with the following commands.
Create a file calledserver.pyand place the following code.
We have created a class withon_getmethod which is HTTP GET method.
The method has two arguments.
One isrequestand another one ifresponse.
You should have guessed what they are by their names themselves.
Therequestargument contains all the information of the incoming request which can be accessed to process the request.
And theresponseargument is used to send the response by setting different things.
Unlike BlackSheep and AIOHTTP we dont have to return a response.
We can use the response and set whatever details we need to send as a response.
We can return the response as JSON by converting the data into JSON usingjson.dumpsmethod.
Lets see an example
Go tohttp://localhost:8000/, you will see the response in JSON.
The request parameters are passed to the HTTP methods as arguments.
you’re free to see the example below for a better understanding.
Thenamepath parameter will be passed toon_getmethod as an argument.
Go tohttp://localhost:8000/Geekflare/and check the response.
We can have as many route parameters as we want.
We can jump into the query parameters usingrequest.get_param(param_name)method.
Check out the example below.
Go tohttp://localhost:8000/?name=Geekflareto check the response.
We can have as many query parameters as we want.
We have seen the GET method in the above example.
The one thing we need to know for other methods is how to access request data.
We can pull up the request data usingrequest.stream.readmethod.
Lets see an example.
Make a POST request with some data to check the output.
Try adding other HTTP methods like DELETE, PUT, etc.., by yourself.
We have converted only the basics of the falcon framework.
But, there is a lot in it.
Go and read thedocsto find out more deeply about it.
Starlette
Starletteis a lightweight ASGI framework in Python.
It has similar almost all the basic features to build server-side applications.
Set up the project with the following commands.
Creating APIs with Starlette is similar to what we have seen in the last frameworks.
The syntax and way of creating APIs are different.
All the concepts remain the same.
So, we are going to include all the things in a single program.
execute the tool with the following command.
Test all things that we have seen in the previous frameworks.
you’re free to learn more about the Starlette framework in itsdocs.
Conclusion
A lot is going on in the Python async landscape these days.
Newframeworksare popping up, old ones are being rewritten, and libraries are being evolved to match async behavior.
But all said and done; Python is production-ready to deliver light-out performance when it comes to web frameworks.
If for so long youve been thinking of migrating to Node, now you dont need to!
Sounds cool?Master Python today!