yeomko22 / TIL

Today I learned
1 stars 0 forks source link

Asgi for web development #96

Open yeomko22 opened 2 years ago

yeomko22 commented 2 years ago

Reference

wsgi limitations

from aiohttp import web

async def hello(request):
    await db.execute("SELECT * FROM tbl")
    return web.Response(text="Hello, world")

app = web.Application()
app.add_routes([web.get('/', hello)])
web.run_app(app)

2019 frameworks

ASGI

async def application(scope, receive, send):
    event = await receive()
    await send(
        {"type": "websocket.send", ...}
    )
async def app(scope, receive, send):
    assert scope["type"] == "http"
    await send({
        "type": "http.response.start",
        "status": 200,
        "headers": [
            (b"content-type", "text/plain"),
        ],
    })
    await send({
        "type": "http.response.body",
        "body": b"Hello, world!",
    })
yeomko22 commented 2 years ago

Referenec

ASGI