Upgrade to Pro — share decks privately, control downloads, hide ads and more …

pip install aiohttp

pip install aiohttp

_amirouche_

July 30, 2017
Tweet

More Decks by _amirouche_

Other Decks in Programming

Transcript

  1. Kesako aiohttp? ▶ asyncio based web framework ▶ HTTP Client

    & Server ▶ Websocket Client & Server ▶ Rich eco-system ▶ Python 3.4 (but 3.5 recommended) Why? Performance Is it worth the trouble? Actually: ▶ It’s very similar to regular Django/Flask ▶ It’s easier when you need realtime features
  2. Héllo World! #!/usr/bin/env python3.5 from aiohttp import web async def

    handle(request): text = "Héllo World!" return web.Response(text=text) app = web.Application() app.router.add_get('/', handle) web.run_app(app)
  3. Where to go from here? ▶ Database → several choices

    (asyncpg, asyncpgsa, aiopg, …) ▶ Templating → jinja2 ▶ REST → you don’t need a framework for that And you have proper nested app support eg. aiohttp-debugtoolbar.
  4. Problem 0: It’s asynchronous, it’s complicated ▶ False: Outside the

    new async/await keywords that you must drop here and there the code looks like its synchronous counterpart. ▶ True: a web handler can be cancelled cf. aiojobs for a way to handle that case.
  5. Problem 1: not monolithic ▶ Client/server API for both regular

    HTTP and websocket. ▶ There’s a low level server API and high level server framework. ▶ You get to choose your prefered tools (databases, template, validation…).
  6. Problem 2: There is not enough resources ▶ There is

    a lot of documentation for aiohttp and other aio libraries ▶ aiohttp maitainers are active on StackOverflow
  7. Problem 3: It’s not Django or Flask Well, that is

    very good thing. Flask and Django are very keen tendency to rely on globals (like PHP 3) which aiohttp fully avoid. This makes the code of aiohttp much more readable and future proof.
  8. Summary ▶ Even if asyncio introduce new concepts to Python

    language its usuability is good and will improve. ▶ aiohttp is not monolithic like its big brother Django. It’s a good thing. Also for the common cases libraries are available.