Basic aiohttp server#


When it comes to deploying an application on a server online, a crucial aspect is its response time since no user wants to wait long for a response. Hence, optimizations and asynchronous processing are the way to go. within this context, aiohttp is a python library that helps implementing asynchronous HTTP Client/Server. The following blog is one of a series of four that will introduce a server implementation, followed by a token based authentication and some stress testing.

What is aiohttp ?#

aiohttp is an HTTP client/server for asyncio. Basically it allows you to write asynchronous clients and servers. The aiohttp package also supports Server WebSockets and Client WebSockets.

What is asyncio?#

asyncio is a library to write concurrent code. It is used at the core of many asynchronous applications to deliver high-performance and speedy processing, etc.

What is concurrency?#

In simple words, concurrency can be described as having multiple computations happening at the same time. In a little more complex formulation, it can be described as the execution of different parts a program out-of-order or in partial order. Hence, you can say concurrency is sort of partial parallelism of computations.

Why is concurrency useful in server/client communications?#

The ability to have multiple units of your code running in parallel allows you to handle more requests or even send multiple calls without requiring more physical resources. In other words, by asynchronously running your functions you can push your system to its limits but you should mind your processing order to avoid unexpected behaviors.

How to implement it?#

Aiohttp includes a variey of examples on how to do this. In the following, I will only include here, what worked best for me:

  1. Start with installing aiohttp using

    Install aiohttp#
    1 pip install aiohttp
    
  2. Write a request handler. Requests in aiohttp are processed using coroutines that will serve as the actual functions called by the client.

    Write a request handler#
    1 import numpy as np
    2 from aiohttp import web
    3
    4 @asyncio.coroutine
    5 def ping(request):
    6     return web.json_response({"text": "pong", "status": "success"})
    
  3. wrap everything by creating an Application instance and registering the request handler

    Create application and register handler#
    1 app = web.Application()
    2 app.add_routes([web.get('/', ping)])
    

Code#

The previously listed steps, should look together in Python as follows:

Framing 1#
1from aiohttp import web
2
3async def ping(request):
4    return web.Response(text="pong")
5
6app = web.Application()
7app.add_routes([web.get('/', ping)])

and you are done, you just create a server application that returns a pong when pong'ed. This code, can require slight changes when creating the application and registering the handler in case of using multi-processing for example.

Testing#

The previous code when executed will start a server running on http://localhost:8080/. To test your server either type in your browser http://localhost:8080/ping or simply curl it using curl http://localhost:8080/ping. The response should be the following pong.

Conclusion#

This blog presented aiohttp and its usefulness within regard to concurrency. It further provided a simple example of a server that will respond with pong if it is alive.

Share this blog#

Updated on 16 April 2022

Last edited on 16.04.2022