기여자인 Andrew Svetlov가 주도하여 개발한 프레임워크 · server 코드는 Flask와 유사한 느낌으로 개발 가능 aiohttp import aiohttp import asyncio async def fetch(session, url): async with session.get(url) as response: return await response.text() async def main(): async with aiohttp.ClientSession() as session: html = await fetch(session, 'https://python.org') print(html) asyncio.run(main()) from aiohttp import web async def handle(request): name = request.match_info.get('name', "Anonymous") text = "Hello, " + name return web.Response(text=text) app = web.Application() app.add_routes([web.get('/', handle), web.get('/{name}', handle)]) web.run_app(app) https://aiohttp.readthedocs.io/en/stable/
콜백은 매번 예외 처리 핸들러를 따로 달아줘야 하지만, 코루틴에서는 여러 Promise 호출을 표준 try-catch 블록으로 묶어서 예외 처리 가능 § Python에서는 asyncio.run()을 통해 코루틴으로 진입한다면, Javascript에서는 Promise API를 통해 코루틴으로 진입 async-await in Javascript async function myfunc() { let resp = await fetch("some-url", { method: "POST", body: JSON.stringify({...}), }); let data = await resp.json(); return data; } myfunc().then((result) => { console.log("result", result); }).catch((err) => { console.log("error", err); });
(대화내역 + 입력칸) · 브라우저 접속 세션 별로 랜덤 user ID 생성 및 할당 (쿠키 + 서버측 세션) · 한 세션에서 타이핑한 메시지가 다른 세션들에서도 모두 보여야 함 § 힌트 : aiohttp, aiohttp_session, aiohttp_sse, aioredis 패키지를 써봅시다 § 변형 : POST + SSE 대신 WebSocket을 사용해서 구현해봅시다 (또는 그 반대) Let's build aiohttp chat app