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

asyncio + aiohttp で作るウェブサービス / How to develop a web service with asyncio and aiohttp

842aaadff6e13fbf372c9b17b49b031c?s=47 Pine Mizune
September 18, 2018

asyncio + aiohttp で作るウェブサービス / How to develop a web service with asyncio and aiohttp

PyCon JP 2018 で発表した資料です。

『 asyncio + aiohttp で作るウェブサービス 』
https://pycon.jp/2018/event/sessions

---

This is the material I presented at PyConJP 2018.

"How to develop a web service with asyncio and aiohttp"
https://pycon.jp/2018/event/sessions

842aaadff6e13fbf372c9b17b49b031c?s=128

Pine Mizune

September 18, 2018
Tweet

Transcript

  1. 1Z$PO +1 4FQ 1JOF.J[VOF ʰ BTZODJP BJPIUUQ ʱͰ࡞Δɺ΢ΣϒαʔϏε )PXUPEFWFMPQBXFCTFSWJDFXJUIBTZODJP BOEBJPIUUQ

  2.   "CPVUNF 8IBUJTBTZODJP )PXUPVTFBJPIUUQ 

  3. "CPVU NF QJOF QJOF 8FCNPCJMFBQQFOHJOFFSJO5PLZP IUUQTFNPKJHFOOJOKB &NPKJ(FOFSBUPSUPPMGPS4MBDL BCPVU ."6 XSJUUFOCZ1ZUIPOBJPIUUQ

  4. &NPKJ (FOFSBUPS MPWFT 1ZUIPO *NBHFQSPDFTTJOH "QQMJDBUJPOTFSWFS %FW0QT

  5. B BTZODJP

  6. 8IBU JTBTZODJP § "TZODISPOVT *0 FWFOUMPPQ BOEDPSPVUJOFT § TJOHMFUISFBEFE §

    MJLF+BWB4DSJQU &4 PS3VCZ`T'JCFS § 5IFTUBOEBSEMJCSBSZ 1ZUIPO § BTZOD BXBJU LFZXPSET 1ZUIPO § )JHIQFSGPSNBODFBOEMJHIUXFJHIU § TVJUBCMFGPS*0CPVOEQSPDFTTJOH !
  7. &WFOU MPPQ § 3VOEFGBVMUFWFOUMPPQ import asyncio loop = asyncio.get_event_loop() loop.run_forever()

    § 6TFPUIFSFWFOUMPPQ MJCVW CBDLFOE import asyncio import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
  8. &WFOU MPPQ § 3VOEFGBVMUFWFOUMPPQ import asyncio loop = asyncio.get_event_loop() loop.run_forever()

    § 6TFPUIFSFWFOUMPPQ MJCVW CBDLFOE import asyncio import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) 4UBSUEFGBVMUFWFOUMPPQGPSFWFS
  9. &WFOU MPPQ § 3VOEFGBVMUFWFOUMPPQ import asyncio loop = asyncio.get_event_loop() loop.run_forever()

    § 6TFPUIFSFWFOUMPPQ MJCVW CBDLFOE import asyncio import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) $IBOHFFWFOUMPPQQPMJDZ
  10. $PSPVUJOFT § &BTZUPVTFDPSPVUJOFT import asyncio async def run(): await asyncio.sleep(1)

    coroutine = run() print(coroutine) #=> <coroutine object coroutine at 0x10872bbf8> loop = asyncio.get_event_loop() loop.run_until_complete(coroutine)
  11. $PSPVUJOFT § &BTZUPVTFDPSPVUJOFT import asyncio async def run(): await asyncio.sleep(1)

    coroutine = run() print(coroutine) #=> <coroutine object coroutine at 0x10872bbf8> loop = asyncio.get_event_loop() loop.run_until_complete(coroutine) 6TFBABTZOD EFGATUBUFNFOU
  12. $PSPVUJOFT § &BTZUPVTFDPSPVUJOFT import asyncio async def run(): await asyncio.sleep(1)

    coroutine = run() print(coroutine) #=> <coroutine object coroutine at 0x10872bbf8> loop = asyncio.get_event_loop() loop.run_until_complete(coroutine) $BMMBDPSPVUJOFGVODUJPO
  13. $PSPVUJOFT § &BTZUPVTFDPSPVUJOFT import asyncio async def run(): await asyncio.sleep(1)

    coroutine = run() print(coroutine) #=> <coroutine object coroutine at 0x10872bbf8> loop = asyncio.get_event_loop() loop.run_until_complete(coroutine) 8BJUVOUJMUIFDPSPVUJOFFOET
  14. 'VUVSFT § .BLF'VUVSF GSPNDPSPVUJOFT improt asyncio async def identity(value): return

    value future1 = asyncio.ensure_future(identity(1)) future2 = asyncio.ensure_future(identity(2)) future3 = asyncio.gather(future1, future2) loop = asyncio.get_event_loop() print(loop.run_until_complete(future3)) #=> [1, 2]
  15. 'VUVSFT § .BLF'VUVSF GSPNDPSPVUJOFT improt asyncio async def identity(value): return

    value future1 = asyncio.ensure_future(identity(1)) future2 = asyncio.ensure_future(identity(2)) future3 = asyncio.gather(future1, future2) loop = asyncio.get_event_loop() print(loop.run_until_complete(future3)) #=> [1, 2] 1SPNJTF %FGGFSFE FUD
  16. 'VUVSFT § .BLF'VUVSF GSPNDPSPVUJOFT improt asyncio async def identity(value): return

    value future1 = asyncio.ensure_future(identity(1)) future2 = asyncio.ensure_future(identity(2)) future3 = asyncio.gather(future1, future2) loop = asyncio.get_event_loop() print(loop.run_until_complete(future3)) #=> [1, 2] $PSPVUJOFT'VUVSFT
  17. 'VUVSFT § .BLF'VUVSF GSPNDPSPVUJOFT improt asyncio async def identity(value): return

    value future1 = asyncio.ensure_future(identity(1)) future2 = asyncio.ensure_future(identity(2)) future3 = asyncio.gather(future1, future2) loop = asyncio.get_event_loop() print(loop.run_until_complete(future3)) #=> [1, 2] .BLFOFXGVUVSFGSPNUXPGVUVSFT
  18. 'VUVSFT § .BLF'VUVSF GSPNDPSPVUJOFT improt asyncio async def identity(value): return

    value future1 = asyncio.ensure_future(identity(1)) future2 = asyncio.ensure_future(identity(2)) future3 = asyncio.gather(future1, future2) loop = asyncio.get_event_loop() print(loop.run_until_complete(future3)) #=> [1, 2] 8BJUVOUJMUIF'VUVSFFOET
  19. B BJPIUUQ

  20. 8IBU JTBJPIUUQ § "TZODISPOPVT)551DMJFOUTFSWFSGSBNFXPSL § BTZODJP CBTFE § 1ZUIPOSFRVJSFE §

    4FQ WSFMFBTFE § $VSSFOUMZW § 4VQQPSUT8FC4PDLFU 4FTTJPO $PPLJF FUD  BJPMJCTBJPIUUQ
  21. )BOEMFST from aiohttp import web async def items(request): page =

    request.query.get(‘page’) items = await item_repository.filter(page) return web.json_response(result) app.router.add_get(‘/items’, items) § .7$XFCBQQGSBNFXPSL`T$POUSPMMFS § SFDFJWFSFRVFTU SFUVSOSFTQPOTF
  22. )BOEMFST from aiohttp import web async def items(request): page =

    request.query.get(‘page’) items = await item_repository.filter(page) return web.json_response(result) app.router.add_get(‘/items’, items) § .7$XFCBQQGSBNFXPSL`T$POUSPMMFS § SFDFJWFSFRVFTU SFUVSOSFTQPOTF "TZODISPOPVT%#BDDFTT
  23. )BOEMFST from aiohttp import web async def items(request): page =

    request.query.get(‘page’) items = await item_repository.filter(page) return web.json_response(result) app.router.add_get(‘/items’, items) § .7$XFCBQQGSBNFXPSL`T$POUSPMMFS § SFDFJWFSFRVFTU SFUVSOSFTQPOTF "EEBOFXSPVUFUPUIFEFGBVMU63-SPVUFS
  24. )BOEMFST from aiohttp import web routes = web.RouteTableDef() @routes.get(‘/items’) async

    def items(request): page = request.query.get(‘page’) items = await item_repository.filter(page) return web.json_response(result) § 4VQQPSU'MBTLMJLFSPVUJOHEFGJOJUJPOT
  25. .JEEMFXBSFT import htmlmin from aiohttp import web @web.middleware async def

    htmlmin_middleware(request, handler): response = await handler(request) if response.content_type == 'text/html': response.text = htmlmin.minify(response.text) return response app.middlewares.append(htmlmin_middleware()) § )5.-NJOJGZNJEEMFXBSFTBNQMF
  26. .JEEMFXBSFT import htmlmin from aiohttp import web @web.middleware async def

    htmlmin_middleware(request, handler): response = await handler(request) if response.content_type == 'text/html': response.text = htmlmin.minify(response.text) return response app.middlewares.append(htmlmin_middleware()) § )5.-NJOJGZNJEEMFXBSFTBNQMF *OKFDUBTZODISPOPVTNJEEMFXBSF
  27. 1SPEVDUJPO § V84(* JTOPUTVQQPSUFE! § OHJOY (VOJDPSO BSFSFDPNNFOEFECZP⒏DJBM § SVOTTFSWFSXJUIDVTUPNXPSLFS

    FOWJSPONFOUT $ gunicorn myapp:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker
  28. $PODMVTJPO B § BTZODJP JTBTZODISPOVT *0 FWFOUMPPQBOEDPSPVUJOFTMJCSBSZ § BJPIUUQ JTBTZODJP

    CBTFEXFCBQQGSBNFXPSL § BJPIUUQ JTTJNQMFBOEDBOCVJMEIJHIQFSGPSNBODFBQQ 5IBOLZPV