åºæ
ãã®èšäºã§ã¯ãå€ãã®ãŠãŒã¶ãŒã«åæã«åœ¹ç«ã€Webã¢ããªã±ãŒã·ã§ã³ã®ããã°ã©ãã³ã°ã®éèŠãªåŽé¢ã«è§ŠããããšæããŸããã€ãŸãããã¹ãŠã®éå±ãªéåæI / Oãå€éåãªã©ããã§ã«åæããŸãã
次ã®ç®æšãè¿œæ±ãããŠããŸãã
- ãã®åéã§è³æãäœç³»åããããã«ãçšèªã®ããã€ãã®ççŸãè°è«ãã
- å€ãã®é¡§å®¢ãµãŒãã¹ã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããåºç€ãå®å šã«å解ããŸãã
- å€ãã®ã¯ã©ã€ã¢ã³ãã«åœ¹ç«ã€å°æ¥ã®Pythonã¢ããªã±ãŒã·ã§ã³ã®æŠç¥ãéçºãã
- é ã®äžã«é®®æãªç»åãäœæããŸãïŒçç±ããªãéãã圌ããããªããç解ããŠãããšèšãããã§ã¯ãããŸãã-説æã§ãããšãïŒ
ãªãã§ïŒ
PHPããã°ã©ãã³ã°ã®é·å¹Žã¯å ±ãããŸãã-ç§ã¯èå°è£ã§äœãèµ·ãã£ãŠããã®ãçåã«æããŸããã§ããã ãããããããžã§ã¯ãããã£ãããšé ããªãå§ããŸãããã確ãã«é ããªããŸãã-ç§ã¯ãããå¿ã«åãæéã§ãããšå€æããPythonãå匷ãå§ããŸããïŒç§ã¯ä»æåããŠããŸãïŒã ãããããforkãããsocketãããeventloopãããmultiprocessingãããepollããªã©ãããããçš®é¡ã®åèªã«ééããªã飜ã飜ãããŠããŸããã ãã£ãšæ·±ãæãäžããããšã«ããŸããã ããããåºãŠãããã®ã¯ããªã次第ã§ãã ããã«ãããããããç¹å®ã®æ··ä¹±ãååšããŸãã ãã®ç¢ºèªã¯ãåèæç®[ 6ã7 ]ã§ç¢ºèªã§ããŸãã
int mainïŒïŒ
ãã®èšäºã§ã¯ãLinux 2.6以éã«åºã¥ããLinux I / Oã«ã€ããŠèª¬æããŸãã çŽ æã¯äž»ã«ç§ã®ãããªäººã«çŠç¹ãåãããŠããããïŒæ°Žãå ç±ããè£ çœ®ã«ã¯ãªãã·ã§ã³ã§é³é¿è¡šç€ºåšãè£ åãããŠããŸãïŒãå€ãã®åºæ¬äºé ã確èªããå¿ èŠããããããå¿ èŠã®ãªãé ç®ã¯ã¹ãããããŠãã ããã
åºæ¬çãªæŠå¿µ
Linuxã§ã¯ã ãã¡ã€ã«ã¯åºæ¬çãªæœè±¡æŠå¿µã§ãã Linuxã¯ããã¹ãŠããã¡ã€ã«ã§ããããšããå²åŠãé å®ããŠããŸããã€ãŸããã»ãšãã©ã®ããåãã¯ãã¡ã€ã«ã®èªã¿åããšæžã蟌ã¿ã«ãã£ãŠå®çŸãããŸãã ãã¡ã€ã«æäœã¯ãäžæã®èšè¿°åïŒãã¡ã€ã«èšè¿°åãŸãã¯fdïŒã䜿çšããŠå®è¡ãããŸãã Linuxã·ã¹ãã ã®ããã°ã©ãã³ã°ã®ã»ãšãã©ã«ã¯ããã¡ã€ã«èšè¿°åã®æäœãå«ãŸããŸãã
éåžžã®ãã¡ã€ã« ïŒéåžžã®ãã¡ã€ã«ïŒããããŸã-ããã¯ç§ãã¡ãæ £ããŠãããã®ïŒéåžžã®æå³ã§æãæ®éã®ããã¡ã€ã«ãïŒã§ããã ç¹æ®ãã¡ã€ã«ã¯ãã¡ã€ã«ãšããŠè¡šããããªããžã§ã¯ãã§ãã Linuxã¯4çš®é¡ã®ç¹æ®ãã¡ã€ã«ããµããŒãããŠããŸãã
- ãããã¯ããã€ã¹ãã¡ã€ã«
- ãã£ã©ã¯ã¿ãŒI / Oããã€ã¹ãã¡ã€ã«
- ååä»ããã€ãïŒååä»ããã€ããŸãã¯FIFOïŒ
- ãœã±ãã
ãœã±ããã¯ãç°ãªãã³ã³ãã¥ãŒã¿ãŒïŒã¯ã©ã€ã¢ã³ããµãŒããŒïŒã«é 眮ã§ãã2ã€ã®ç°ãªãããã»ã¹éã®éä¿¡ãæäŸãããããåŸè ããŸãã«ç§ãã¡ã®é¢å¿ã®ç¯å²ã§ãã å®éããããã¯ãŒã¯ããã°ã©ãã³ã°ãšã€ã³ã¿ãŒãããã®ããã°ã©ãã³ã°ã¯ãœã±ããäžã«æ§ç¯ãããŠããŸãã
æåã®è¿äŒŒã§ã¯ãéåžžã®ãã¡ã€ã«ãšãœã±ããã®ã¿ãèæ ®ããã ãã§ååã§ãã
å ¥å/åºåã¢ãã«
åèšãããšãUnixã©ã€ã¯ã·ã¹ãã ã§ã¯ã5 + 1ã®ç°ãªãI / Oã¢ãã«ãå©çšå¯èœã§ãã ããã©ã¹ã¯ã³ãã¯å°ãåŸã§èª¬æããŸãããä»ã®ãšãããåã¢ãã«ããã詳现ã«æ€èšããŠãã ããã
ããããã³ã°I / OïŒããããã³ã°I / OïŒ
ããã©ã«ãã§ã¯ããã¹ãŠã®å ¥åã¯ãããã¯ã¹ã¿ã€ã«ã§åºåãããŸãã å ¥å/åºåã®ãããã¯ã§çºçããããã»ã¹ã®æŠç¥å³ãæ€èšããŠãã ããã

ãã®å Žåãããã»ã¹ã¯recvfromã·ã¹ãã ã³ãŒã«ãè¡ããŸãã ãã®çµæãããŒã¿ãå°çããã·ã¹ãã ã³ãŒã«ããããã¢ããªã±ãŒã·ã§ã³ãããã¡ã«æžã蟌ããŸã§ãããã»ã¹ã¯ãããã¯ãããŸãïŒã¹ãªãŒãç¶æ ã«ãªããŸãïŒã
ãã®åŸãã·ã¹ãã ã³ãŒã«ã¯çµäºãïŒOKãè¿ãïŒãããŒã¿ãåŠçã§ããŸãã
æããã«ããã®ã¢ãããŒãã«ã¯éåžžã«å€§ããªæ¬ ç¹ããããŸã-ããŒã¿ãåŸ æ©ããŠããéïŒããã³æ¥ç¶ã®å質ãªã©ã®ããã«éåžžã«é·ãæéããããå ŽåããããŸãïŒãããã»ã¹ã¯ã¹ãªãŒããããªã¯ãšã¹ãã«å¿çããŸããã
ãã³ããããã³ã°I / OïŒãã³ããããã³ã°I / OïŒ
ãœã±ãããæäœãããšãã«éãããã¯ã¢ãŒããèšå®ããå®éã«æ¬¡ã®ããšãã«ãŒãã«ã«äŒããŸãïŒãããã»ã¹ãããã¯ã«æµžãïŒã¹ãªãŒãïŒããã«ãå®è¡ãããå ¥å/åºåãäžå¯èœãªå Žåããããã¯ããã«ãããè¡ãããšãã§ããªããšãããšã©ãŒãè¿ããŸãããã³ããããã³ã°å ¥åºåã§çºçããããã»ã¹ã®æŠç¥å³ã

readã«ã·ã¹ãã ã³ãŒã«ãéä¿¡ããæåã®3åã¯ãçµæãè¿ããŸããã ã«ãŒãã«ã¯ããŒã¿ããªãããšãèªèããEWOULDBLOCKãšã©ãŒãè¿ããŸãã
æåŸã®ã·ã¹ãã ã³ãŒã«ã¯æåããŸã ããŒã¿ãèªã¿åãæºåãã§ããŸããã ãã®çµæãã«ãŒãã«ã¯ããã»ã¹ãããã¡ã«ããŒã¿ãæžã蟌ã¿ãåŠçã«äœ¿çšã§ããããã«ãªããŸãã
ããã«åºã¥ããŠãéãããã¯ã¢ãŒãã§éããŠãããœã±ããã«å¯ŸããŠåžžã«recvfromïŒããŒã¿ãèŠæ±ïŒãåŒã³åºãã«ãŒããäœæã§ããŸãã ãã®ã¢ãŒãã¯ããŒãªã³ã°ãšåŒã°ããŸãã ã¢ããªã±ãŒã·ã§ã³ã¯ãããŒã¿ã®å¯çšæ§ã«ã€ããŠã·ã¹ãã ã®ã³ã¢ãåžžã«ããŒãªã³ã°ããŸãã åºæ¬çã«ãããã€ãã®ãœã±ãããé çªã«ããŒãªã³ã°ããŠãããŒã¿ãããæåã®ãœã±ããããèªã¿åãããã®å¶éã¯ãããŸããã ãã®ã¢ãããŒãã¯ã倧ããªãªãŒããŒãããïŒããã»ããµã®ãªãŒããŒãããïŒã«ã€ãªãããŸãã
I / Oã®å€éåïŒI / Oã®å€éåïŒ
äžè¬çã«ãå€éåãšããèšèã¯ãã³ã³ãã¯ãããšè§£éãããŸãã æé管çã®ã¢ãããŒããã以äžã®ããšãåŠã¶ãã§ããŸã説æã§ãããšæããŸãã I / Oãå€éåããå ŽåãOSã§äœ¿çšå¯èœãªã·ã¹ãã ã³ãŒã«ã®1ã€ïŒselectãpollãpselectãdev / pollãepollïŒLinuxã«æšå¥šïŒãkqueueïŒBSDïŒãªã©ã®ãã«ããã¬ã¯ãµãŒïŒããªã³ã«ãããããã¯ãã代ããã«ãããã¯ããŸãå®éã®I / OåŒã³åºãã æŠç¥çã«ã¯ãå€éåããã»ã¹ãç»åã«ç€ºãããŠããŸãã

ãœã±ãããèªã¿åãå¯èœã«ãªãã®ãåŸ ã€select'aãåŒã³åºããšãã¢ããªã±ãŒã·ã§ã³ããããã¯ãããŸãã ãã®åŸãã«ãŒãã«ã¯èªã¿åãå¯èœãªã¹ããŒã¿ã¹ãè¿ãã recvfromã䜿çšããŠããŒã¿ãååŸã§ããŸãã äžèŠ-å®å šãªå€±æã åãããã¯ãåŸ æ©ãããã«2ã€ã®ã·ã¹ãã ã³ãŒã«ïŒselectããã³recvfromïŒ-é«ããªãŒããŒãããã ãã ããããããã³ã°æ¹åŒãšã¯ç°ãªããselectïŒããã³ãã®ä»ã®ãã«ããã¬ã¯ãµïŒã䜿çšãããšã1ã€ã§ã¯ãªãè€æ°ã®ãã¡ã€ã«èšè¿°åããã®ããŒã¿ãæåŸ ã§ããŸãã ããã¯ãç¹ã«ãªãœãŒã¹ãéåžžã«éãããŠããå Žåãå€ãã®é¡§å®¢ã«ãµãŒãã¹ãæäŸããæãåççãªæ¹æ³ã§ãããšèšããªããã°ãªããŸããã ããã¯ãªãã§ããïŒ ãã«ããã¬ã¯ãµãããŠã³ã¿ã€ã ïŒã¹ãªãŒãïŒãæžããããã§ãã ç§ã¯æ¬¡ã®ç»åã説æããããšããŸã

ãœã±ããã«å¯Ÿå¿ããèšè¿°åã®ããŒã«ãäœæãããŸãã æ¥ç¶äžã«EINPROGRESSå¿çãå±ããå Žåã§ããããã¯æ¥ç¶ã確ç«ãããŠããããšãæå³ããŸãã ãã¹ãäžã®ãã«ããã¬ã¯ãµã¯ãæåã«è§£æŸããããã®ãåŒãç¶ã䜿çšããŸãã
ä»æ³šç®ïŒ æãéèŠãªããšïŒ
質åã«çããŠãã ããïŒã©ã®ã€ãã³ããããå¯èœæ§ãé«ãã§ãã ã€ãã³ãAã®å ŽåãããŒã¿ã¯ç¹å®ã®ãœã±ãããŸãã¯ã€ãã³ãBã®æºåãã§ããŠããããšãããŒã¿ã¯å°ãªããšã1ã€ã®ãœã±ããã®æºåãã§ããŠããããšïŒ ã åçïŒB
å€éåã®å Žåããã¹ãŠã®ãœã±ãããã«ãŒãã§ãã§ãã¯ãããæåã®ãœã±ãããæºåãããŸãã 圌ãšä»äºãããŠããéãä»ã®äººãéã«åãããã«å°çããããšãã§ããŸããã€ãŸããã¢ã€ãã«æéã®æéãççž®ããŸãïŒæåã«é·ãæéåŸ ã€ããšãã§ããŸãããæ®ãã®æéã¯ãã£ãšçããªããŸãïŒã
ïŒããããã³ã°ã䜿çšããŠïŒéåžžã®æ¹æ³ã§åé¡ã解決ããå Žåãã©ã®æ¥ç¶ããæåã®1ç§ã2ç§ãèªã¿åãããªã©ãæšæž¬ããå¿ èŠããããŸãã ã€ãŸã ç§ãã¡ã¯100ïŒ ééããŠåŸ ã£ãŠããŸãããããŠãã®æéãç¡é§ã«ããããšã¯ã§ããŸããã§ããã
ã¹ã¬ãã/åããã»ã¹ã®I / OïŒã¹ã¬ãããŸãã¯ããã»ã¹ããšã«1ã€ã®ãã¡ã€ã«èšè¿°ïŒ
æåã«5 + 1ã®æ¹æ³ããããšèšããŸãããããã¯ãããããããããã³ã°I / Oãå®è¡ãããè€æ°ã®ã¹ããªãŒã ãŸãã¯ããã»ã¹ã䜿çšãããå Žåã®ã¢ãããŒãã§ããã I / Oå€éåã«äŒŒãŠããŸãããããã€ãã®æ¬ ç¹ããããŸãã Linuxã®ã¹ã¬ããã¯ïŒããããã·ã¹ãã ã³ãã³ãã䜿çšãããšïŒéåžžã«é«äŸ¡ãªã®ã§ãã¹ã¬ããã䜿çšãããšãªãŒããŒããããå¢å ããŸãã ããã«ãPythonãããã°ã©ãã³ã°èšèªãšèŠãªãå ŽåãPythonã«ã¯GILãå«ãŸããŠããããããã£ãŠãåç¬éã«1ã€ã®ããã»ã¹å ã§å®è¡ã§ããã¹ã¬ããã¯1ã€ã ãã§ãã å¥ã®ãªãã·ã§ã³ã¯ãããããã³ã°ã¹ã¿ã€ã«ã§I / OãåŠçããåããã»ã¹ãäœæããããšã§ãã ãã ããããã»ã¹éã®çžäºäœçšïŒIPC-ããã»ã¹ééä¿¡ïŒã«ã€ããŠèããå¿ èŠããããããã«ã¯ããã€ãã®å°é£ã䌎ããŸãã ããã«ãã³ã¢ã®ç·æ°ã1ãè¶ ããªãå Žåããã®ã¢ãããŒãã«ã¯çãããå©çããããŸãã ãšããã§ãç§ãç¥ãéããApacheã¯ãã®ããã«åäœãïŒMPMããªãã©ãŒã¯ãŸãã¯ã¹ã¬ããïŒãã¹ã¬ãããŸãã¯å¥ã®ããã»ã¹ã§ã¯ã©ã€ã¢ã³ãã«ãµãŒãã¹ãæäŸããŸãã
å ¥åä¿¡å·é§ååºåïŒä¿¡å·é§åI / OïŒ
ã·ã°ãã«ã䜿çšããŠããããã¯ããã«ããŒã¿ãèªã¿åãããšãå¯èœã«ãªã£ããšãã«ïŒãã³ãã«ãèªã¿åãæºåãã§ããïŒã«ãŒãã«ã«SIGIO圢åŒã®ã·ã°ãã«ãéä¿¡ãããããšãã§ããŸãã æŠç¥çã«ããã®ã¢ãããŒãã¯ç»åã«ç€ºãããŠããŸãã

ãŸããã·ã°ãã«ãæäœããããã®ãœã±ãããã©ã¡ãŒã¿ãŒãèšå®ããsigactionã·ã¹ãã ã³ãŒã«ã䜿çšããŠã·ã°ãã«ãã³ãã©ãŒãå²ãåœãŠãå¿ èŠããããŸãã çµæã¯å³åº§ã«è¿ããããããã¢ããªã±ãŒã·ã§ã³ã¯ãããã¯ãããŸããã å®éãã³ã¢ããã¹ãŠã®äœæ¥ãåŠçããŸãã ããŒã¿ã®æºåãã§ãããã¢ãã¿ãŒããSIGIOã·ã°ãã«ãéä¿¡ããŸããSIGIOã·ã°ãã«ã¯ãããã«ã€ã³ã¹ããŒã«ããããã³ãã©ãŒïŒã³ãŒã«ããã¯é¢æ°ãã³ãŒã«ããã¯ïŒãåŒã³åºããŸãã ãããã£ãŠãrecvfromåŒã³åºãèªäœã¯ãã·ã°ãã«ãã³ãã©ãŒãŸãã¯ã¡ã€ã³ããã°ã©ã ã¹ããªãŒã ã§è¡ãããšãã§ããŸãã ç§ãç¥ãéããããã«ã¯1ã€ã®åé¡ããããŸãããã®ã¿ã€ãã®ããã»ã¹ããšã«ã·ã°ãã«ã¯1ã€ãããããŸããã ã€ãŸã äžåºŠã«1ã€ã®fdãã䜿çšã§ããŸããïŒç¢ºãã§ã¯ãããŸãããïŒ
éåæI / OïŒéåæI / OïŒ
éåæã®å ¥åºåã¯ãç¹å¥ãªã·ã¹ãã ã³ãŒã«ã䜿çšããŠå®è¡ãããŸãã ããã¯åçŽãªã¢ã€ãã¢ã«åºã¥ããŠããŸã-ã«ãŒãã«ã«ã¯ãæäœãéå§ããI / Oæäœãå®å šã«å®äºãããšãã«ïŒã·ã°ãã«ã䜿çšãããªã©ããŠïŒéç¥ããã³ãã³ããäžããããŸãïŒããŒã¿ã®ããã»ã¹ãããã¡ãŒãžã®ã³ããŒãå«ãïŒã ããã¯ããã®å®è£ ãšä¿¡å·ã®å®è£ ã®äž»ãªéãã§ãã æŠç¥çã«ã¯ãéåæå ¥ååºåã®ããã»ã¹ãç»åã«ç€ºãããŠããŸãã

ã·ã¹ãã ã³ãŒã«aio_readãäœæããå¿ èŠãªãã¹ãŠã®ãã©ã¡ãŒã¿ãŒãæå®ããŸãã æ®ãã®äœæ¥ã¯ãã³ã¢ã«ãã£ãŠè¡ãããŸãã ãã¡ãããI / Oãå®äºããããšãããã»ã¹ã«éç¥ããã¡ã«ããºã ãå¿ èŠã§ãã ãããŠãããã§å€ãã®åé¡ãæœåšçã«çºçããŸãã ãããããã®å¥ã®æéã«ã€ããŠã®è©³çŽ°ã
äžè¬çã«ããã®çšèªã«ã¯å€ãã®åé¡ããããŸãããªã³ã¯ã®äŸã¯ãã§ã«äžããããŠããŸãã å€ãã®å Žåãéåæãéããããã³ã°ãããã³å€éåãããå ¥ååºåã®éã«ã¯æŠå¿µã®æ··ä¹±ããããŸãããéåæãã®æŠå¿µãã®ãã®ãããŸããŸãªæ¹æ³ã§è§£éã§ããããã§ãã ç§ã®ç解ã§ã¯ãéåæãšã¯æéçã«ç¬ç«ããŠããããšãæå³ããŸãã ã€ãŸããæã¡äžããããåŸã圌ã¯ãããæºãããããŸã§åœŒã®äººçãçãããããŠç§ãã¡ã¯ãã çµæãåŸã
å®éã«
å®éã«ã¯ãã¿ã¹ã¯ã«åºã¥ããç°ãªãã¢ãã«ã®çµã¿åããã 次ã®ããã«ããŸãã
- ããã¯ä»ãã®åæäœã«ã¹ã¬ãã/ããã»ã¹ã䜿çšããŸã-æçã§ã¯ãããŸãã
- ç°ãªãã¹ã¬ãã/ããã»ã¹ã®å€ãã®ã¯ã©ã€ã¢ã³ãã åã¹ã¬ãã/ããã»ã¹ã¯ãã«ããã¬ã¯ãµã䜿çšããŸã
- ç°ãªãã¹ã¬ãã/ããã»ã¹ã®å€ãã®ã¯ã©ã€ã¢ã³ãã 䜿çšãããéåæå ¥å/åºåïŒaioïŒ
- ãµãŒããŒãã«ãŒãã«ã«åã蟌ãã ãã§ã
C10Kåé¡ã®è©³çŽ°
ãŸãšã
æ··ä¹±ãããããã®ã®éãã«ã€ããŠãå°ãªããšãå°ãã¯æããã«ãªãããšãé¡ã£ãŠããŸãã
ãŸããã¯ããå€éåã«ãŒã«ïŒaioãçµäºãããŸã§ïŒã
åèæç®ã調ã¹ãŠã¿ããšããœã±ãããšã¯ç°ãªããéåžžã®ãã¡ã€ã«ãéããããã³ã°ã¢ãŒãã«è»¢éã§ããªããšããçµè«ã«éããŸããã Aioã¯åœŒãã®ããã«å©çšå¯èœã§ããããã§ããããã¯ããã§è°è«ãããŸãïŒ Linuxäžã®éåæI / OãŸãã¯å°çãžãããã
æç®ãšåèæç®
- ãããŒãã©ããLinuxã ã·ã¹ãã ããã°ã©ãã³ã°
- W.ãªãã£ãŒãã¹ãã£ãŒãã³ã¹ããã«ãã§ããŒãã¢ã³ããªã¥ãŒM.ã©ãããUnix Network Programming Volume 1ã第3çïŒãœã±ãããããã¯ãŒãã³ã°APIã
- Stevens R.ãRago S. Unixã ãããã§ãã·ã§ãã«ããã°ã©ãã³ã°ã第2ç
- ã¿ããªã®ãæ°ã«å ¥ãC10Kåé¡
- Linuxã§ã®éåæI / OãŸãã¯å°çãžãããã
- 2ã€ã®é«æ§èœI / Oèšèšãã¿ãŒã³ã®æ¯èŒ
- éåæãšéããããã³ã°
- ããããã³ã°ãš ãã³ããããã³ã°ãœã±ãã