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

HTTPとASGI Webサーバの関係

Junya Fukuda
February 20, 2021

HTTPとASGI Webサーバの関係

Python Charity Talks in Japan 2021.02 の登壇資料です。

Junya Fukuda

February 20, 2021
Tweet

More Decks by Junya Fukuda

Other Decks in Technology

Transcript

  1. •෱ా ൏໵ʢJunya Fukudaʣʢ@JunyaFffʣ •גࣜձࣾ೔ຊγεςϜٕݚʢJSLʣॴଐ ௕໺ݝͷձࣾ •GEEKLAB.NAGANO - ίϛϡχςΟεϖʔεӡӦ •Effective Python

    ͷಡॻձ΍ͬͯ·͢ʙ •ʢ͓͠͝ͱͰʣόϦϡʔϒοΫε •ຊ޷͖ʹѪ͞ΕΔαʔϏεΛ໨ࢦͯ͠ʢݹຊങऔɾൢചʣ ΪʔΫϥϘ௕໺ಡॻձ
  2. ASGI •Asynchronous Server Gateway Interface ͷུ •Webαʔό͕ΞϓϦέʔγϣϯͷ async/await ʹରԠ •WSGIͷޙܧ

    •WebαʔόͱΞϓϦέʔγϣϯͷΠϯλʔϑΣʔεͷ࢓༷ https://www.youtube.com/watch?v=EwG5EWKu8nw ASGIʢඇಉظαʔόήʔτ΢ΣΠΠϯλʔϑΣʔεʣͷ֓ཁ (Junya Fukuda) [PyCon JP 2020]
  3. ASGI •Asynchronous Server Gateway Interface ͷུ •Webαʔό͕ΞϓϦέʔγϣϯͷ async/await ʹରԠ •WSGIͷޙܧ

    •WebαʔόͱΞϓϦέʔγϣϯͷΠϯλʔϑΣʔεͷ࢓༷ https://www.youtube.com/watch?v=EwG5EWKu8nw ASGIʢඇಉظαʔόήʔτ΢ΣΠΠϯλʔϑΣʔεʣͷ֓ཁ (Junya Fukuda) [PyCon JP 2020] •ෳ਺ͷҰൠతͳϓϩτίϧΛॲཧՄೳʮHTTP/1.1ʯʮHTTP/2ʯʮWebsocketʯ
  4. HTTP/2 •ετϦʔϜ ‣ ΫϥΠΞϯτ ‣ αʔόʔ ‣ ΫϥΠΞϯτ ‣ αʔόʔ

    ‣ 1ͭͷTCPͷίωΫγϣϯͷதͰॱ൪଴ͪΛ͢Δ͜ͱͳ͘ɺฒྻʹॲཧ HTTP/1.1 HTTP/2 ฒྻʹॲཧ
  5. HTTP/2 •ετϦʔϜ ‣ ΫϥΠΞϯτ ‣ αʔόʔ ‣ ΫϥΠΞϯτ ‣ αʔόʔ

    HTTP/1.1 HTTP/2 ฒྻʹॲཧ ‣ 1ͭͷTCPͷίωΫγϣϯͷதͰॱ൪଴ͪΛ͢Δ͜ͱͳ͘ɺฒྻʹॲཧ
  6. HTTP/2 対応 ASGI Web Server •ASGI Web ServerͷҰཡ Daphne Mangum

    IUUQTHJUIVCDPN fl PSJNPOENBODBBXFTPNFBTHJ
  7. HTTP/2 対応 ASGI Web Server •ASGI Web ServerͷҰཡ Daphne Mangum

    IUUQTHJUIVCDPN fl PSJNPOENBODBBXFTPNFBTHJ
  8. HTTP/2 対応 ASGI Web Server •ASGI WebServer ͱ͍͑͹ Uvicorn •·ͩHTTP/2ະରԠ

    ʢରԠ༧ఆ͋Γʣ Uvicorn currently supports HTTP/1.1 and WebSockets. Support for HTTP/2 is planned. Uvicorn͸ݱࡏɺHTTP/1.1ͱWebSocketsΛαϙʔτ͍ͯ͠·͢ɻ HTTP/2ͷαϙʔτ͕༧ఆ͞Ε͍ͯ·͢ɻ IUUQTXXXVWJDPSOPSH
  9. HTTP/2 対応 ASGI Web Server •ASGI࢓༷ͷରԠঢ়گ Framework HTTP WebSockets Server

    Push WebSocket HTTP Hypercorn 1 ◦ 2 ◦ 1 ◦ 2 ◦ ◦ ◦ Uvicorn 1 ◦ 2 × 1 ◦ 2 × × × Daphne 1 ◦ 2 ◦ 1 ◦ 2 × × × Mangum 1 ◦ 2 × 1 ◦ 2 × × ×
  10. HTTP/2 対応 ASGI Web Server •ASGI࢓༷ͷରԠঢ়گ Framework HTTP WebSockets Server

    Push WebSocket HTTP Hypercorn 1 ◦ 2 ◦ 1 ◦ 2 ◦ ◦ ◦ Uvicorn 1 ◦ 2 × 1 ◦ 2 × × × Daphne 1 ◦ 2 ◦ 1 ◦ 2 × × × Mangum 1 ◦ 2 × 1 ◦ 2 × × ×
  11. HTTP/2 対応 ASGI Web Server •ϋΠύʔίʔϯͱಡΈ·͢ •WSGI Web αʔόͷGunicornʹΠϯεύΠΞ͞Ε͍ͯΔ •HTTP

    / 1ɺHTTP / 2ɺWebSocketʢHTTP / 1͓ΑͼHTTP / 2ܦ༝ʣαϙʔτ •worker ͷλΠϓʹ asyncio,uvloop,trio ΛࢦఆՄೳ •worker ਺ΛࢦఆՄೳ
  12. 処理 requestButton.onclick = async function() { result = []; for

    (let i = 0; i < 24; i++) { result.push(fetch(`/message/${i}/`)); } await Promise.all(result); }
  13. HTTP/1.1 HTTP/2 ⽐較 αʔό HTTP/1.1 HTTP/2 •Hypercorn •fastapi 0.61.1 0.11.0

    Webαʔό Web Framework ⁶ ⁶ ʢASGIʣ ʢHTTP/2ʣ ʢHTTP/1.1ʣ
  14. HTTP/1.1 HTTP/2 ⽐較 αʔό HTTP/1.1 HTTP/2 •Hypercorn •fastapi 0.61.1 0.11.0

    Webαʔό Web Framework ⁶ ⁶ ʢASGIʣ ʢHTTP/2ʣ ʢHTTP/1.1ʣ
  15. HTTP/1.1 HTTP/2 ⽐較 •h2load ίϚϯυ Φϓγϣϯ ҙຯ -n ૯ϦΫΤετ਺ -c

    ΫϥΠΞϯτ਺ɻಉ࣌ΞΫηε਺ -p ϓϩτίϧʢࣗಈ൑ผʣ •h2load -c 100 -n 10000 https://127.0.0.1:8000/query
  16. HTTP/1.1 HTTP/2 ⽐較 ҎԼ3ͭ৚݅ •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100 •ΫϥΠΞϯτಉ࣌઀ଓ 50,

    ϦΫΤετ਺ 5000 •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000 ࢦඪ •ॲཧͷऴྃ·Ͱʹ͔͔ͬͨ࣌ؒ •1ඵ͋ͨΓͷॲཧͨ͠ϦΫΤετ਺
  17. HTTP/1.1 HTTP/2 ⽐較 ҎԼ3ͭ৚݅ •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100 •ΫϥΠΞϯτಉ࣌઀ଓ 50,

    ϦΫΤετ਺ 5000 •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000 ࢦඪ •ॲཧͷऴྃ·Ͱʹ͔͔ͬͨ࣌ؒ •1ඵ͋ͨΓͷॲཧͨ͠ϦΫΤετ਺ → ୹͍ͱྑ͍ → ଟ͍ͱྑ͍
  18. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100
  19. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100      ϦΫΤετ [s]
  20. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  21. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  22. HTTP/1.1 HTTP/2 ⽐較      •ΫϥΠΞϯτಉ࣌઀ଓ 50,

    ϦΫΤετ਺ 5000      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  23. HTTP/1.1 HTTP/2 ⽐較      •ΫϥΠΞϯτಉ࣌઀ଓ 100,

    ϦΫΤετ਺ 10000      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  24. HTTP/1.1 HTTP/2 ⽐較 •h2load — help Max concurrent streams to

    issue per session. When http/1.1 is used, this speci fi es the number of HTTP pipelining requests in- fl ight. ηογϣϯ͝ͱʹൃߦ͢Δ࠷େಉ࣌ετϦʔϜ਺ɻ http/1.1 Λ࢖༻͢Δ৔߹ɺ HTTP ύΠϓϥΠϯϦΫΤετ ͷ਺Λࢦఆ͠·͢ɻ -m, --max-concurrent-streams=<N>
  25. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100, ετϦʔϜ 5
  26. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100, ετϦʔϜ 5      ϦΫΤετ [s]
  27. HTTP/1.1 HTTP/2 ⽐較 ऴྃ࣌ؒ [s]     

    •ΫϥΠΞϯτಉ࣌઀ଓ 10, ϦΫΤετ਺ 100, ετϦʔϜ 5      ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  28. HTTP/1.1 HTTP/2 ⽐較      •ΫϥΠΞϯτಉ࣌઀ଓ 50,

    ϦΫΤετ਺ 5000, ετϦʔϜ 5      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  29. HTTP/1.1 HTTP/2 ⽐較      •ΫϥΠΞϯτಉ࣌઀ଓ 100,

    ϦΫΤετ਺ 10000, ετϦʔϜ 5      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  30. HTTP/1.1 HTTP/2 ⽐較 ετϦʔϜͳ͠ ετϦʔϜ͋Γ     

    •ΫϥΠΞϯτಉ࣌઀ଓ 100, ϦΫΤετ਺ 10000, ετϦʔϜ 5 ετϦʔϜͳ͠ ετϦʔϜ͋Γ      ऴྃ࣌ؒ [s] ϦΫΤετ [s] HTTP/1.1 HTTP/2 HTTP/1.1 HTTP/2
  31. •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ

    •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ → ଎౓͕վળͷՄೳੑ
  32. •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ

    •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ
  33. •HTTP/2Կ͕͓͍͍͠ͷʁ •HTTP/2ͷWebserver Hypercorn ʹ͢Δ͜ͱͰ଎౓վળ͕ΈΒΕΔ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython

    Web Application Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ → ଎౓͕վળͷՄೳੑ
  34. •HTTP/2Կ͕͓͍͍͠ͷʁ •Typeʹ fetch ΍ xhr ͕ଟ͍ •ࣗ͝਎ͷPython Web Application Λ͝ཡʹͳͬͯΈ͍ͯͩ͘͞ɻ

    •ϒϥ΢βͷσόοΨΛ։͍ͯϦϩʔυ •Protocol=HTTP/1.1ʹͳ͍ͬͯΔʢLBͷ಺ଆ͕HTTP/1.1ͷՄೳੑ΋ʣ ͔΋͠Ε·ͤΜ •HTTP/2ͷWebserver Hypercorn ʹ͢Δ͜ͱͰ଎౓վળ͕ΈΒΕΔ → ଎౓͕վળͷՄೳੑ
  35. •AWSͰσϓϩΠ͍ͯ͠Δ৔߹ •ALB ʹ ಺ଆ͸HTTP/2 ະରԠͷͨΊɺHTTP/̍.1 •NLB ʹ 2020೥6݄ʹ಺ଆHTTP/2ରԠ͍ͯ͠Δ ϒϥ΢β Ϧόʔε

    ϓϩΩγ Hyper corn Web FW )551 )551 "4(* IUUQTBXTBNB[PODPNKQBCPVUBXTXIBUTOFXOFUXPSLMPBECBMBODFSOPXTVQQPSUTUMTBMQOQPMJDJFT
  36. 2 •WSGIΞϓϦέʔγϣϯͷ৔߹ •HypercornʢASGI Webαʔό͸WSGIରԠ͍ͯ͠·͢ʣͰσϓϩΠՄೳ •ࢼͯ͠ΈΔՁ஋͕͋Δ͔΋ʂʁ •Django 2ܥ - wsgi.pyΛϥοϓ͢Δ͚ͩ from

    hypercorn.middleware import AsyncioWSGIMiddleware import sampleproject.wsgi as wsgi_app asyncio_app = AsyncioWSGIMiddleware(wsgi_app.application) https://github.com/jrfk/django2-hypercorn
  37. 参考資料 •How to serve HTTP/2 using Python IUUQTNFEJVNDPNQZUIPOQBOEFNPOJVNIPXUPTFSWFIUUQVTJOHQZUIPOFCCEF ff 

    •HTTP/1 should die IUUQTNFEJVNDPN!QHKPOFTIUUQTIPVMEEJFCEF •Hypercorn IUUQTQHKPOFTHJUMBCJPIZQFSDPSOJOEFYIUNM •jrfk / fastapi-hypercorn_Performance-measurement IUUQTHJUIVCDPNKSGLGBTUBQJIZQFSDPSO@1FSGPSNBODFNFBTVSFNFOU
  38. HTTP/3 現在 •IETF QUIC WGͰٞ࿦ → ͍Α͍Α RFC ΁ •HTTP/3

    QUIC ঝೝ •RFC ൃߦ४උஈ֊ - ผͷHTTPηϚϯςΟΫε࢓༷ʹґଘ͍ͯ͠ΔͨΊ •࣮ͦͯ͠૷΁…
  39. HTTP/3 Python •quicwg ͷ GithubϦϙδτϦ Wiki - Implementations •aioquic -

    QUIC implementation using Python and asyncio. •ASGI͸… HTTP/3 ʹؔͯ͠ͷϊʔλον
  40. HTTP/3 Python •quicwg ͷ GithubϦϙδτϦ Wiki - Implementations •aioquic -

    QUIC implementation using Python and asyncio. •ASGI͸… HTTP/3 ʹؔͯ͠ͷϊʔλον ͕͔ͩ͠͠…
  41. HTTP/3 Python •quicwg ͷ GithubϦϙδτϦ Wiki - Implementations •aioquic -

    QUIC implementation using Python and asyncio. •ASGI͸… HTTP/3 ʹؔͯ͠ͷϊʔλον ͕͔ͩ͠͠…
  42. HTTP/3 Python •quicwg ͷ GithubϦϙδτϦ Wiki - Implementations •aioquic -

    QUIC implementation using Python and asyncio. •ASGI͸… HTTP/3 ʹؔͯ͠ͷϊʔλον ͕͔ͩ͠͠… Hypercorn can optionally serve the current draft of the HTTP/3 speci fi cation using the aioquic library.
  43. Hypercorn aioquic Ͱ͸࣮ࡍʹಈ͔ͯ͠Έ·͠ΐ͏ h3spec w IZQFSDPSORVJDCJOEMPDBMIPTUDFSU fi MFDFSUQFNLFZ fi MF

    LFZQFNCJOEMPDBMIPTUTJNQMFGBTUBQJBQQ w ଓ͍ͯITQFDͰ͢ w ਅͬ੺Ͱͨ͠ʜ
  44. Hypercorn aioquic Ͱ͸࣮ࡍʹಈ͔ͯ͠Έ·͠ΐ͏ h3spec w IZQFSDPSORVJDCJOEMPDBMIPTUDFSU fi MFDFSUQFNLFZ fi MF

    LFZQFNCJOEMPDBMIPTUTJNQMFGBTUBQJBQQ w ଓ͍ͯITQFDͰ͢ w ਅͬ੺Ͱͨ͠ʜ w )ZQFSDPSOͰ࢖༻͍ͯ͠ΔBJPRVJDͰ΋ಉ༷ʹਅͬ੺Ͱͨ͠
  45. - HTTP/3 Python実装 現状 •Python HTTP/3͸͜Ε͔Βʂ •pythonͷasyncioʹ͸τϥϯεϙʔτ૚ʢUDP΋ʣΛѻ͏API͕༻ҙ͞Ε͍ͯΔ •Hypercorn΋aioquic΋։ൃ͸׆ൃ •HTTP/3 QUICͱasyncioͰASGIΛ͞ΒʹҰาઌ΁

    •ASGIͰ͸HTTP/3 QUICͰ͸ٞ࿦͸࢝·͍ͬͯ·ͤΜ͕ɺ •·ͣ͸Ұาͣͭ •ΠγϡʔΛͷ͍ͧͯΈΔͱ͜Ζ͔Β͸͡ΊͯΈͯ͸ 👀 •Hypercornͷissueʹίϝϯτͯ͠Έ·ͨ͠
  46. 参考資料 •HTTP/3 explained IUUQTIUUQFYQMBJOFEIBYYTFKB •Fastly : Making the Web faster

    with HTTP/3 - kazuho oku 8FCΛՃ଎͢Δ)551 •http3-note IUUQTHJUIVCDPN fl BOPZVLJIUUQOPUF •Playing with QUIC - The Chromium Projects IUUQTXXXDISPNJVNPSHRVJDQMBZJOHXJUIRVJD