Skillversitas

Asynchronous Flask

The async is the async def: the view (the await, the no the block), the I/O (the http, the DB, the no the the), and the event loop (the 1, the no the thread). The aiohttp is the client (the await, the no the requests), the asyncpg is the DB (the pool, the no the the), and the no is the all (the CPU, the 1, the no the the). This chapter covers the async view (the await, the no the block), the aiohttp (the client, the no the requests), the async DB (the pool, the no the the), and the no-all (the CPU, the 1).

The async view and the aiohttp

# the ASYNC VIEW (the await,
#   the no block, the no the 1):
from flask import Flask

app = Flask(__name__)

#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
@app.route("/weather/")
async def weather():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    async with aiohttp.ClientSession(
    ) as s:
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        async with s.get(
                "https://api.weather/
                /x") as r:
            data = await r.json()
            #   the no: the no (
            #     the no, the no,
            #     the no, the no
            #     the no, the no)
    return jsonify(data)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the NO (the 1, the block,
#   the no the async, the no
#   the the):
@app.route("/sync/")
def sync_view():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    time.sleep(1)
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    return "the sync"
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The async view is the 1: the async def is the await (the no the block, the no the 1, the no the the), and the aiohttp is the client (the await, the no the requests, the no the 1) - the async with is the 2 (the session, the 1, the no the the), and the no is the 1 (the event the loop, the no the 1, the no the the). The no is the 1: the time.sleep is the block (the 1, the no the 1, the no the the), and the asyncio.sleep is the no (the await, the 1, the no the the) - the DB is the next: the pool the no the the.

The async DB and the no-all

# the ASYNC DB (the pool, the
#   no the the, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
pip install asyncpg
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
import asyncpg

pool = await asyncpg.create_pool(
    dsn, min_size=2,
    max_size=10)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

@app.route("/api/count/")
async def count():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    async with pool.acquire() \
            as conn:
        n = await conn.fetchval(
            "SELECT the FROM post")
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
    return jsonify(count=n)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the NO-ALL (the CPU, the 1,
#   the no the the, the no
#   the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The async DB is the pool: the asyncpg is the 1 (the pip, the no the the), the create_pool is the 2 the 10 (the 1, the no the the), and the fetchval is the await (the 1, the no the 1, the no the the) - the no is the SQLAlchemy the sync (the asyncpg, the 1, the no the the). The no the all is the 1: the CPU is the 1 (the compute, the no the 1, the no the the), the I/O is the async (the http, the DB, the no the the), and the no is the 1 (the mix, the 1, the no the the) - the rule is the one: the async def the await, the aiohttp the client, the pool the DB, and the no the CPU - the next chapter's structure is the folder the 1 the no the the.

The Working Rules

  • The async def is the await, the no the block. The view the 1 (the no the 1, the no the the), and the aiohttp the client (the await, the no the requests, the no the 1) - the async with the 2 (the session, the 1, the no the the), and the no the 1 (the event, the loop, the no the 1).
  • The no is the block, the sleep the no. The time.sleep the 1 (the block, the no the 1, the no the the), and the asyncio.sleep the no (the await, the 1, the no the the) - the no the 2 (the no the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The pool is the 2 the 10, the no the the. The asyncpg the 1 (the pip, the no the the), and the create_pool the 2 (the 1, the no the the) - the fetchval the await (the 1, the no the 1, the no the the), and the no the SQLAlchemy (the sync, the 1, the no the the).
  • The no-all is the 1, the CPU the no. The CPU the 1 (the compute, the no the 1, the no the the), and the I/O the async (the http, the DB, the no the the) - the no the 1 (the mix, the 1, the no the the), and the no the 2 (the no the 1, the no the the).

Summary

The async is the async def: the view the await (the no the block, the no the 1), the aiohttp the client (the await, the no the requests, the async with the 2), and the no the block (the time.sleep, the asyncio.sleep the no, the no the the). The async DB is the pool (the asyncpg, the create_pool the 2 the 10, the fetchval the await, the no the SQLAlchemy the sync), and the no-all is the 1 (the CPU the compute, the I/O the async, the no the 1). The rule is the one: the async def the await, the aiohttp the client, the pool the DB, and the no the CPU - the next chapter's structure is the folder: the app/ the 1, the extensions.py the no the import, and the config.py the 3 (the no the the, the no the 1).