Asynchronous Python — let’s take a look at aioHTTP
Tags: Aiohttp, Python, Python3, Asynchronous

There are several framework standards at Python web development — Django, Flask, probably Tornado. But what do you hear when applying on vacancy breaking days? “Do you have an experience with aioHTTP?”

Well I was the one, who’ve heard a lot about aioHTTP and I’m going to overview entry points to aioHTTP world, which surprised me the most.

Installation

The main site looks pretty simple, that makes it attractive to try the package (https://docs.aiohttp.org). IMHO, the best package documentation that I’ve ever seen is boto3 (AWS integration) (https://boto3.amazonaws.com). It’s really short and simple enough, but meanwhile it covers all the things, which you may catch up with. But let’s get back to the main actor.

Of course, package installation is perfectly easy and usually I do quickstart article right after the installation. The first interesting notice, that i’ve found — you can use aioHTTP not only as backend server, but also create a client server. I’ll concentrate on backend server. Server supports several ways of routes definition, you can use class-based routes definition, which I found extremely descriptive, compact and clear to understand:

routes = web.RouteTableDef() @routes.view(“/view”)

class MyView(web.View):

async def get(self):

…

async def post(self):

…

app.router.add_routes(routes)

So it shouldn’t be any issues if you try to create a simple web server just doing the documentation steps. But they may happen after.

ORM

When you do Flask developing, you will probably use SQLAlchemy. Or if you prefer NoSQL — MongoEngine. I’m passionate about them both. But since we want to be asynchronous, we do need extra ORM for our needs. Thanks to amazing aioHTTP we have this possibility — you can find an integration packages right on the Third-Party libraries page. Currently I see Postgres, MySQL and Redis are all on the party, but here is the tricky moment that i’ve faced with — usually I use SQLite starting any pet project, since I don’t need the power of it’s big brothers. But there is no driver for it. Let’s do a quick googling. Here it is — aiosqlite. Why developers don’t add it to the main docks?

On this step you may firstly realize one of the most disappointing moment in aioHTTP — it’s power is still uncovering. Core developers already made tons of work creating asynchronous representation of all required packages (DB integration, testing, etc.). But thousand tons are still need to be done. In this particular example — of course you have an integration package to PostgreSQL, but it’s a low-level integration. No syntactic sugar which you have using Django ORM, or MongoEngine (by the way there is no official integration with MongoDB either). You can do what you need with DB, but you’ll probably spend a lot of time implementing the DB integration in a way you prefer by writing some wrappers over the given drivers.

Testing

I like tests. You like tests. Or you will like tests one day. Because all the roads come to Rome. To fall in love with tests they should be easy to set up, easy to write and easy to maintain. Sounds easy?

I prefer pytest — test DB during runtime, a lot of syntactic sugar by pre-defined assertion methods (assertEqual, assertIn), descriptive coverage report — this and a lot of the other things are really charming. I’ve spent a day trying to run pytest-aiohttp with fake DB and trying to find pre-defined assertion methods which i’ve mentioned above. No luck. As a result additional docker container with test DB, additional time by writing so frequently used assertion methods. I hope someone would be more lucky than me (if so — share your experience)

Contribution

Django repository has 1864 contributors and 27967 commits; Flask has 567 contributors and 3841 commits, aioHTTP has 468 contributors and 6890 commits. All of them have nearest commits during the past month. It means — they are all alive. Repositories should be alive, community should ask questions, create additional packages — developer wants to count on support starting a new project from scratch.

Third-party libraries are also the thing, which you want to count on starting something new. And unfortunately here we have worth statistics — almost all third-parties, that were written for aioHTTP have not been updating for several months. It scares me. My only hope, that community will activate itself and start committing what personally I want to start doing.