å æ¥ã django-jaã®Discord ã§åºã話é¡ã§ãããDjangoã§ä½æããã¢ããªãæ¬çªç°å¢ã§åããéã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã«ã¤ãã¦ã§ãã
å ·ä½çã«ã¯WSGIã¨ASGIã©ã¡ãã使ãã®ããããã¦ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã¯ã©ããããã®ãã
WSGIãASGIã
DjangoãWSGIãASGIã®ã©ã¡ãã§åãããã§ãããè¿·ãããããªãç¾ç¶ã¯WSGIã§ããã§ãã
ãASGIã¯å¿ è¦ã«ãªã£ãã使ãããããã®æ°æã¡ã§ããã®ããããããã¾ãã
ASGIã¯ãPythonã®Asyncï¼éåæï¼ã«å¯¾å¿ããããã®ãã¢ããªã±ã¼ã·ã§ã³ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãã
WSGIãããããããçç±ã¯æ¬¡ã®éãã
- åæå¦çãªã®ã§ãããã°ãããã
ASGIãããããããªãçç±ã¯æ¬¡ã®éãã
- éåæå¦çã«ãªãã®ã§ãããã°ãã¥ãã
- ã¨ã«ããããã§ããIOå¾ ã¡ã®ç¶æ³ã§ã¯ä¸¦è¡ã§å®è¡ããã¦ã³ã³ãã¥ã¼ã¿ãªã½ã¼ã¹çã«ã¯å¹çè¯ãããã§ãããã¨ã©ã¼ã®ã¨ãã®ã¹ã¿ãã¯ãã¬ã¼ã¹ãèªã¿ã¥ããã§ããå®è¡é åºãã¢ããªã«ãã£ã¦ã¯ãã©ãã©ã«ãªããã¨ããããã¨ã©ã¼ã®åç¾ããã¥ããã§ãã
- ã¾ã Pythonæ¨æºä»æ§ã§ã¯ãªãï¼ããããã¡ã¯ãã¹ã¿ã³ãã¼ãã«ã¯ãªã£ã¦ããï¼
- ç©æ¥µçã«æ¡ç¨ããã»ã©ã§ããªããã¨ããæã
ASGIãããããããç¶æ³ã¯ãç¾ç¶ã ã¨éå®çããªãã¨æã£ã¦ã¾ãã
- Django Channelsã使ã£ã¦ããå ´å
- ã¢ããªãASGI対å¿ãAsyncã®ã³ã¼ãã«ãªã£ã¦ããå ´å
- ããããasync/awaitã使ã£ãã¢ããªã®å ´åã¯ãASGIãµã¼ãã¼ã§åãããªãã¨æ©æµããã¾ããªãããããã®å ´åãASGIãµã¼ãã¼ãå©ç¨ãããã¨ã«ãªãã§ãããã
åè: docs.djangoproject.com
Djangoã§asyncãawaitã使ã£ãå ´åãI/Oå¾ ã¡ã®é¨åã§ã¯éåæå¦çã«ãã£ã¦é«éåãæå¾ ã§ãã¾ããããããCPUä¾åã®å¦çã«ã¤ãã¦ã¯ç¹ã«éããªãããã§ã¯ããã¾ããã
ãããWSGIã¢ããªã±ã¼ã·ã§ã³ã®ããã©ã¼ãã³ã¹ãä¸ãããå ´åã次ã®ãããªæ¹æ³ã試ãã¦ãã©ãã«ããªããªãå ´åã«ããããéåæIOãASGIãµã¼ãã¼ãæ¤è¨ããããããã§ããããªã¨æãã¾ãã
- ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã®è¨å®è¦ç´ã
- ããã»ã¹æ°ãå¢ãã
- ã¹ã¬ããæ°ãå¢ãã
- WSGIãµã¼ãã¼ã®å¤æ´ãæ¤è¨
- gunicornã®å ´åã¯uWSGIã«å¤ãã¦ã¿ãããªã©ã
- DBã¯ã¨ãªã®æé©å
- n+1ã¯ã¨ãªããªãããé度ã®é ãã¯ã¨ãªãæ¹åãã
- DBèªä½ã®æé©å
- ãµã¼ãã¼æ§è½ãä¸ãã¦é«éã«ãã
- ã¤ã³ããã¯ã¹ãä½ã£ã¦ã¯ã¨ãªãéããªãããã«ãã
- å¤é¨éä¿¡åæ°ã®åæ¸
- DBãå¤é¨ã¹ãã¬ã¼ã¸ãªã©ã¨ã®éä¿¡ã¬ã¤ãã³ã·ã®å½±é¿ãæ¸ããã¦é«éåãã
- å¤é¨å¼ã³åºãé¨åã®éåæã¿ã¹ã¯å
- ããã§ããéåæã¿ã¹ã¯åã¯ãCeleryã使ã£ããããããå¦çã«ããã¨ããæå³
ããã«è¨è¼ããæ¹æ³ä»¥å¤ã«ããã¦ã§ãã¢ããªèªä½ã®ããã©ã¼ãã³ã¹æ¹åæ¹æ³ãè²ã ããã®ã§ãããããä¸éããã£ããã¨ã«ãéåæIOã«ã¤ãã¦èãã¦ã¿ããããã§ããããã
ããããã®WSGIãµã¼ãã¼
WSGIãµã¼ãã¼ã¯å¥½ã¿ãããã®ã§ã人ã«ãã£ã¦çãã¯ç°ãªãã¾ãã
å人çã«ã¯Gunicornãé·ã使ã£ã¦ãã¦ã2024å¹´ç¾å¨ã§ãããããããããã¦ãã¾ãã
å人çã«æ°ã«å ¥ã£ã¦ãç¹ã¯æ¬¡ã®éãã§ãã
- Gunicornèªä½ã®ã³ã¼ãã¯Pure Pythonã§æ¸ããã¦ãã
- ä½ãä¸å ·åã®ã¨ãã«èª¿æ»ãããã
- ã»ããã¢ããã§ããããå°ãªã
- ãã«ãããã»ã¹ï¼ãã«ãã¹ã¬ããã§ã®åä½ã«å¯¾å¿ãã¦ãã
- Workerã¹ã¿ã¤ã«ã§ã®åä½ã®ãããã¢ããªã±ã¼ã·ã§ã³ãä¸å®å®ã§ããã»ã¹ãè½ã¡ã¦ãèªååèµ·åãããã
ä¸æ¹ãæ°ãã¤ããç¹ã¯ä»¥ä¸ã§ãã
- Linuxã§ããåããªãï¼ããããã³ã³ããã§åãããªãæè¿ã¯ãã¾ãåé¡ã«ãªããªãã§ãã..ï¼
- Pure Pythonãªã®ã§ãéçã¾ã§CPUãªã½ã¼ã¹ã使ãåãããã¢ããªã«ã¯åããªã
- uWSGIã®ããã«æ©è½ãè±å¯ã§ã¯ãªãï¼ãµã¤ãã«ã¼çã«è²ã åããã¨ãã¯uWSGIã®ã»ããç°¡åï¼
ããããã®ASGIãµã¼ãã¼
AWGIãµã¼ãã¼ã«ã¤ãã¦ã¯ãããããã§ããã»ã©ã®éç¨å®ç¸¾ããã¦ãã¦ãã¾ã å人çã«ã¯å°ãªãã§ããéç¨å®ç¸¾ã¯ãã¾ãæ¥æ¿ã«å¢ãããã¨ããªãããã§ãã
èªç¤¾å ã¨ãã¦ã¯Uvicornã使ã£ã¦ãé¨åãå¤ãã§ãããã ããDaphneãHypercornãè©ä¾¡ãæ¯è¼ããããã§ããªãã§ãã
Uvicornã使ã£ã¦ããçç±ã¨ãã¦ã¯ãGunicornã®Workerã¨ãã¦åä½ããããã¨ãã§ããããã§ãã ããã¯ã¤ã¾ããGunicornã®ããã»ã¹ç®¡çæ©è½ããã®ã¾ã¾ä½¿ããã¨ãããã¨ã§ãã
ã¢ããªã±ã¼ã·ã§ã³èµ·å ã§Workerããã»ã¹ãè½ã¡ã¦ãèªååèµ·åã§ãã¾ãã
Daphneã§åæ§ã®ãã¨ããããå ´åã¯ãå¥éSupervisorãªã©ãä½µç¨ããã°ããã®ã§ãããGunicornï¼Uvicornã§åãPythonããã»ã¹ã®ä¸ã§å®çµãããªããã®ã»ãã楽ããªã¨ããå°è±¡ãæã£ã¦ã¾ãã