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

asyncio + aiohttp で作るウェブサービス / How to develo...

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

Pine Mizune

September 18, 2018
Tweet

More Decks by Pine Mizune

Other Decks in Programming

Transcript

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

    MJLF+BWB4DSJQU &4 PS3VCZ`T'JCFS § 5IFTUBOEBSEMJCSBSZ 1ZUIPO § BTZOD BXBJU LFZXPSET 1ZUIPO § )JHIQFSGPSNBODFBOEMJHIUXFJHIU § TVJUBCMFGPS*0CPVOEQSPDFTTJOH !
  2. &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())
  3. &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
  4. &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
  5. $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)
  6. $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
  7. $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
  8. $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
  9. '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]
  10. '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
  11. '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
  12. '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
  13. '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
  14. 8IBU JTBJPIUUQ § "TZODISPOPVT)551DMJFOUTFSWFSGSBNFXPSL § BTZODJP CBTFE § 1ZUIPOSFRVJSFE §

    4FQ WSFMFBTFE § $VSSFOUMZW § 4VQQPSUT8FC4PDLFU 4FTTJPO $PPLJF FUD  BJPMJCTBJPIUUQ
  15. )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
  16. )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
  17. )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
  18. )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
  19. .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
  20. .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
  21. 1SPEVDUJPO § V84(* JTOPUTVQQPSUFE! § OHJOY (VOJDPSO BSFSFDPNNFOEFECZP⒏DJBM § SVOTTFSWFSXJUIDVTUPNXPSLFS

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

    CBTFEXFCBQQGSBNFXPSL § BJPIUUQ JTTJNQMFBOEDBOCVJMEIJHIQFSGPSNBODFBQQ 5IBOLZPV