The aiohttp framework

The aiohttp (http://aiohttp.readthedocs.io/) framework is a popular asynchronous framework based on the asyncio library, which has been around since the first days of the library.

Like Flask, it provides a request object and a router to redirect queries to functions that handle them.

The asyncio library's event loop is wrapped into an Application object, which handles most of the orchestration work. As a microservice developer, you can just focus on building your views as you would do with Flask.

In the following example, the api() coroutine returns some JSON response when the application is called on /api:

    from aiohttp import web 
 
    async def api(request): 
        return web.json_response({'some': 'data'}) 
  
    app = web.Application() 
    app.router.add_get('/api', api) 
    web.run_app(app) 

The aiohttp framework has a built-in web server, which is used to run this script via the run_app() method, and, overall, if you are used to Flask, the biggest difference is that you do not use decorators to route requests to your views.

This framework provides helpers like those you find in Flask, plus some original features such as its Middleware, which will let you register coroutines to perform specific tasks such as custom error handling.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset