1.ã¯ããã«
åé¡ãã§ããã ãæ··ä¹±ãããããã«ã解決çãããã°ã©ããŒã«ä»»ããŠãã ãã;ïŒãããããçå£ã«ãç§ã®æèŠã§ã¯ãã³ããã³ã«ãåæ§ã®ããšãèµ·ãããŸãããªããªããåãã§ãã©ããã«ãããããããããã¯ç¶æ³ãææ§ã«ããããã«äœ¿çšãããããã§ããåŸè ã¯ãã©ãã«ãè¡ããªã䞊åããã°ã©ãã³ã°ã®åé¡ããŸã ãããšããäºå®ã«ãã£ãŠç¹åŸŽä»ãããããããŠæãéèŠãªããšã«ãã³ã«ãã³ã¯ãããã®åºæ¬çãªè§£æ±ºã«è²¢ç®ããŸããã
çšèªããå§ããŸãããã ã圌ãã¯äœåäžçã«èªã£ãããšãããããããããŸã§ã®ãšãããäžçãã¯éåæããã°ã©ãã³ã°ãšäžŠåããã°ã©ãã³ã°ã®éãã«ã€ããŠçåãæããããŠããïŒ[1]ã®éåæã®ãããã¯ã«é¢ããè°è«ãåç §ïŒãéåææ§ãšäžŠåæ§ãç解ããåé¡ã®æ žå¿ã¯ã䞊åæ§èªäœãå®çŸ©ããããšããå§ãŸããŸããããã¯åã«ååšããŸãããããçš®ã®çŽæçãªç解ããããããã¯ãã°ãã°ç°ãªãæ¹æ³ã§è§£éãããŸãããã2ãš2ãã®æäœã®çµæã«ã€ããŠã®è°è«ã»ã©å»ºèšçã«ãã¹ãŠã®è³ªåãåãé€ãç§åŠçå®çŸ©ã¯ãããŸããã
ãŸããããããã¹ãŠååšããªããããçšèªãšæŠå¿µãæ··åãããŠããããã䞊åããã°ã©ãã³ã°ãšåæããã°ã©ãã³ã°ãéåæããã°ã©ãã³ã°ããªã¢ã¯ãã£ãããã°ã©ãã³ã°ãªã©ãåºå¥ããŠããŸããçFelixã®ãããªæ©æ¢°åŒèšç®æ©ããœãããŠã§ã¢èšç®æ©ãšã¯ç°ãªãåäœãããããšã«æ°ä»ãã®ã«åé¡ãçããå¯èœæ§ã¯äœããšæããŸããããããæ£åŒãªèŠ³ç¹ãããããªãã¡äžé£ã®æäœãšæçµçµæã§ã¯ããããã®éã«éãã¯ãããŸããããã®ååã¯ã䞊åããã°ã©ãã³ã°ã®å®çŸ©ã§èæ ®ã«å ¥ããå¿ èŠããããŸãã
ãäžåšçšãªãFelixããœãããŠã§ã¢èšç®æ©ã®ãããªäžè²«ããçµæã«ã€ãªããã䞊ååŠçãèšè¿°ããããã®å³å¯ãªå®çŸ©ãšééçãªæ段ãå¿ èŠã§ãã ã䞊ååŠçãã®æŠå¿µããã®å®è£ æ段ïŒåãã³ã¢ã®æ°ïŒã«é¢é£ä»ããããšã¯äžå¯èœã§ãããããŠããå éšãã«ãããã®-ããã¯ãäž»ã«ãæ©æ¢°ãã®å®è£ ã«åŸäºããŠãã人ã ãã«é¢å¿ãããããã®ãããªåŸæ¥ã®ã䞊åèšç®æ©ãã䜿çšããŠãã人ã«ã¯é¢å¿ããªãã¯ãã§ãã
ããããç§ãã¡ã¯ç§ãã¡ãæã£ãŠãããã®ãæã£ãŠããŸãããããŠãæµè¡ã§ã¯ãªãã«ããŠããç§ãã¡ã¯ãã³ã«ãã£ã³ãšéåæããã°ã©ãã³ã°ã«ã€ããŠæŽ»çºãªè°è«ãããŠããŸãããããŠããã«ãã¹ã¬ããã«ããããããŠããããã«èŠããŠããä»ã«äœããæäŸãããŠããªãå Žåã¯ãä»ã«äœããã¹ãã§ããããã圌ãã¯ããçš®ã®éæ³ã«ã€ããŠãã話ããŸã;ïŒããããããªããçç±ãç解ããã°ãã¹ãŠãæããã«ãªããŸãããããŠããããã¯ãŸãã«ããã«ãããŸã-䞊ååŠçã®é¢ã«ãããŸãããã®å®çŸ©ãšãã®å®è£ ã
ããããããã°ã©ãã³ã°ã®ç§åŠïŒãã以æ¥ã®ã³ã³ãã¥ãŒã¿ãŒïŒã®ã°ããŒãã«ãªããããŠããçšåºŠã¯å²åŠçãªé«ããããç§ãã¡ã®ã眪深ãå°çããžãšéããŠãããŸããããããã§ã¯ãçŸåšäººæ°ã®ããã³ããªã³èšèªã®ã¡ãªãããæãªãããšãªããPythonèšèªãžã®æ ç±ãèªããããšæããŸãããããããã€ãããããŠä»ã®ç¶æ³ã§ã¯ãç§ã®å¥œã¿ã¯å€ããã§ãããããå®éããããŸã§ã®ãšãããã¹ãŠãããã§ãã
ããã«ã¯ããã€ãã®çç±ããããŸãããã®äžã«ã¯ãPythonãžã®ç¡æã¢ã¯ã»ã¹ããããŸããããã¯æ匷ã®è°è«ã§ã¯ãããŸãããåãQtã®äŸã§ã¯ãç¶æ³ã¯ãã€ã§ãå€ããå¯èœæ§ããããšè¿°ã¹ãŠããŸããããããPythonã¯Kotlinãšã¯ç°ãªããå°ãªããšãJetBrainsãšåãPyCharmç°å¢ã®åœ¢ã§ç¡æã§ããïŒç¹ã«æè¬ããŸãïŒãç§ã®åæ ã¯ãã®åŽã«ãããŸãããŸããã€ã³ã¿ãŒãããäžã®Pythonã®äŸãªã©ãæè²çã§éåžžã«çŸå®çãªãã·ã¢èªã®æç®ã倧éã«ããããšãé åçã§ããã³ããªã³ã§ã¯ããããã¯ãã®æ°ã§ã¯ãªãããããã®å€æ§æ§ã¯ããã»ã©å€§ãããããŸããã
ããããå°ãå ã«ããœãããŠã§ã¢ã®äžŠååŠçãšéåææ§ã®å®çŸ©ãšå®è£ ã®åé¡ã®ã³ã³ããã¹ãã§Pythonããã¹ã¿ãŒããçµæãæ瀺ããããšã«ããŸãããããã¯èšäº[2]ã«ãã£ãŠéå§ãããŸãã..ãä»æ¥ã¯ããžã§ãã¬ãŒã¿ãŒ-ã³ã«ã«ãŒãã³ã®ãããã¯ã«ã€ããŠæ€èšããŸããããããžã®ç§ã®èå³ã¯ãç¹å®ã®ãèå³æ·±ãããçŸæç¹ã§ã¯ããŸã銎æã¿ã®ãªããçŸä»£ã®èšèª/ããã°ã©ãã³ã°èšèªã®å¯èœæ§ãèªèããå¿ èŠæ§ã«ãã£ãŠåºæ¿ãããŠããŸãã
ç§ã¯å®éã«ã¯çŽç²ãªC ++ããã°ã©ããŒãªã®ã§ãããã¯å€ãã®ããšã説æããŠããŸããããšãã°ãPythonã®ã³ããã³ãšãžã§ãã¬ãŒã¿ãŒãé·ãéååšããŠããå ŽåãC ++ã§ã¯ãŸã ãã®å°äœãç²åŸããŠããŸãããããããC ++ã¯æ¬åœã«ãããå¿ èŠãšããŸããïŒç§ã®æèŠã§ã¯ãããã°ã©ãã³ã°èšèªã¯åççã«æ¡åŒµããå¿ èŠããããŸãã C ++ã¯å¯èœãªéãåŒã£åŒµã£ãããã§ãä»ã¯æ¥ãã§è¿œãã€ãããã«ããŠããŸããããããåæ§ã®åæå®è¡ã®åé¡ã¯ãcoroutines / coroutinesãããåºæ¬çãªä»ã®æŠå¿µãã¢ãã«ã䜿çšããŠå®è£ ã§ããŸãããããŠããã®å£°æã®èåŸã«èšèã ãããªããšããäºå®ã¯ãããã«ç€ºãããŸãã
ç§ãã¡ããã¹ãŠãèªããã®ã§ããã°ãç§ã¯C ++ã«é¢ããŠããªãä¿å®çã§ããããšãèªããŸãããã¡ããããã®ãªããžã§ã¯ããšOOPæ©èœã¯ç§ã«ãšã£ãŠãç§ãã¡ã®ãã¹ãŠãã§ãããç§ã¯ãèšããŸã§ããªãããã³ãã¬ãŒãã«æ¹å€çã§ãããŸããç§ã¯åœŒãã®ç¬ç¹ã®ãé³¥ã®èšèªããå®éã«èŠãããšããããŸããã§ãããããã¯ãèŠããšãããã³ãŒãã®èªèãšã¢ã«ãŽãªãºã ã®ç解ãéåžžã«è€éã«ããŸããæã ç§ã¯åœŒãã®å©ãã«é ŒãããšããããŸããããçæ¹ã®æã®æã§ããããã¹ãŠã«ååã§ããç§ã¯STLã©ã€ãã©ãªãå°éãããããªãã§ã¯å®è¡ã§ããŸãã:)ãããã£ãŠããã®äºå®ããã§ãããã³ãã¬ãŒãã«ã€ããŠçåãæã€ããšããããŸããã ããç§ã¯ãŸã ã§ããéãããããé¿ããŠããŸãããããŠä»ãç§ã¯C ++ã®ããã³ãã¬ãŒãcoroutinesããéããªããåŸ ã£ãŠããŸã;ïŒ
Pythonã¯å¥ã®åé¡ã§ãããã³ãã¬ãŒããå ¥ã£ãŠããããšã«ãŸã æ°ã¥ããŠããããèœã¡çããŸãããããããã®äžæ¹ã§ãããã¯å¥åŠãªããšã«ãææ ®ãã¹ãããšã§ããããããKotlinã³ãŒããç¹ã«ãã®ãšã³ãžã³ã³ã³ããŒãã¡ã³ãã³ãŒããèŠããšãäžå®ã¯ããã«æ¶ããŸã;ïŒããããããã¯ãŸã ç¿æ £ãšç§ã®åèŠã®åé¡ã ãšæããŸããæéãçµã€ã«ã€ããŠãããããé©åã«èªèããããã«èªåèªèº«ãèšç·Žããããšãé¡ã£ãŠããŸãïŒãã³ãã¬ãŒãïŒã
ããã...ã³ã«ãã£ã³ã«æ»ããŸããçŸåšã圌ãã¯ã³ã«ãã£ã³ãšããååã§åŒã°ããŠããããšãããããŸãããååã®å€æŽã§äœãæ°ãããªããŸãããïŒã¯ããå®éã«ã¯äœããããŸãããåãšåãããã«ãã»ããã¯é çªã«èæ ®ãããŸãå®è¡ãããæ©èœã以åãšåãããã«ãé¢æ°ãçµäºããåããã ãäœæ¥ãå®äºããåã«ãæ»ãç¹ãåºå®ãããåŸã§äœæ¥ãåéãããŸããåãæ¿ãé åºãæå®ãããŠããªããããããã°ã©ããŒèªèº«ãç¬èªã®ã¹ã±ãžã¥ãŒã©ãŒãäœæããŠãã®ããã»ã¹ãå¶åŸ¡ããŸããå€ãã®å Žåãããã¯é¢æ°ã®ã«ãŒãã¹ã«ãŒã«ãããŸãããããšãã°ãOlegMolchanovã«ãããããªã®RoundRobinã€ãã³ããµã€ã¯ã«[3]ãªã©ã
ããã¯ãã³ã«ã«ãŒãã³ãšéåæããã°ã©ãã³ã°ã®ææ°ã®çŽ¹ä»ãéåžžãæã§ãã©ã®ããã«èŠãããã§ãããã®ãããã¯ã«æ²¡é ãããšãæ°ããçšèªãæŠå¿µãçãŸããããšã¯æããã§ãããžã§ãã¬ãŒã¿ãŒã¯ãã®1ã€ã§ããããã«ããããã®äŸã¯ã䞊åèšå®ãã瀺ãããã®åºç€ã«ãªããŸããããã§ã«ç§ã®èªå解éã«ãããŸãã
2.ããŒã¿ãªã¹ãã®ãžã§ãã¬ãŒã¿
ã ãã-ãžã§ãã¬ãŒã¿ãéåæããã°ã©ãã³ã°ãšã³ããã³ã¯ããã°ãã°ãããã«é¢é£ä»ããããŠããŸãã Oleg Molchanovããã®äžé£ã®ãããªã¯ãããããã¹ãŠã«ã€ããŠèªã£ãŠããŸãããã®ããã圌ã¯ãžã§ãã¬ãŒã¿ãŒã®éèŠãªæ©èœããåååæ¢ããã®ãšåãå Žæããå®è¡ãç¶ç¶ããããã«é¢æ°ã®å®è¡ãäžæåæ¢ããæ©èœããšåŒãã§ããŸãïŒè©³çŽ°ã«ã€ããŠã¯[3]ãåç §ïŒããããŠãããã§ã¯ããã§ã«ããªãå€ãã³ããã³ã®å®çŸ©ã«ã€ããŠäžèšã§è¿°ã¹ãããšãèãããšãæ°ãããã®ã¯äœããããŸããã
ããããçµå±ã®ãšããããžã§ãã¬ãŒã¿ãŒã¯ããŒã¿ã®ãªã¹ããäœæããããã®éåžžã«ç¹æ®ãªçšéãèŠã€ããŸããããã®ãããã¯ã®çŽ¹ä»ã¯ãEgorov Artem [4]ã®ãããªã§ãã§ã«åãäžããããŠããŸãã..ãããããã©ãããããã®ãããªã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠãç§ãã¡ã¯è³ªçã«ç°ãªãæŠå¿µãã€ãŸãæäœãšããã»ã¹ãæ··ãåãããŠããŸããèšèªã®èšè¿°æ©èœãæ¡åŒµããããšã«ãããçºçããå¯èœæ§ã®ããåé¡ã倧éšåç解ããŸããããã§ã¯ã圌ããèšãããã«ãããŸããã¬ã€ããªãã§ãã ãããããŒã¿ãèšè¿°ããããã«generators-coroutinesã䜿çšããããšã¯ããŸãã«ããã«è²¢ç®ããŠããããã«ç§ã«ã¯æããŸãã Oleg Molchanovã¯ããžã§ãã¬ãŒã¿ãŒãããŒã¿æ§é ã«é¢é£ä»ããããšã«å¯ŸããŠãèŠåããããžã§ãã¬ãŒã¿ãŒã¯é¢æ°ã§ãããããšã匷調ããŠããããšã«æ³šæããŠãã ãã[3]ã
ãã ãããžã§ãã¬ãŒã¿ã䜿çšããŠããŒã¿ãå®çŸ©ããããšã«æ»ããŸãããªã¹ãã¢ã€ãã ãèšç®ããããã»ã¹ãäœæããããšãé ãã®ã¯é£ããã§ãããããã£ãŠãããã»ã¹ãªã©ã®ãªã¹ãã«ã€ããŠã¯ããã«çåãçããŸããããšãã°ãå®çŸ©äžãã³ã«ãã³ããäžæ¹åãã§ããæ©èœããªãå Žåãã©ã®ããã«åå©çšããã®ã§ãããããããã»ã¹ã«ã€ã³ããã¯ã¹ãä»ããããšãã§ããªãå Žåããã®ä»»æã®èŠçŽ ãèšç®ããã«ã¯ã©ãããã°ããã§ããïŒçãçArtemã¯ãããã®è³ªåã«çããããšã¯ãªãããªã¹ãã®èŠçŽ ãžã®ç¹°ãè¿ãã®ã¢ã¯ã»ã¹ã¯æŽçã§ããã玢åŒä»ãã¯åãå ¥ããããªããšèŠåããã ãã§ããã€ã³ã¿ãŒãããã§æ€çŽ¢ãããšãç§ã«ãåæ§ã®è³ªåãããã ãã§ãªããææ¡ãããŠãã解決çã¯ããã»ã©ç°¡åã§æçœã§ã¯ãªãããšãããããŸãã
ãã1ã€ã®åé¡ã¯ããªã¹ãçæã®é床ã§ããããã§ãåcoroutineã¹ã€ããã§ãªã¹ãã®åäžèŠçŽ ã圢æããŸããããã«ãããããŒã¿çææéãé·ããªããŸãã ãããããã§èŠçŽ ãçæããããšã«ãããããã»ã¹ãå€§å¹ ã«å éã§ããŸããããããããããããã«ã¯åé¡ããããŸãããã§ã«å®è¡äžã®ããã»ã¹ãåæ¢ããã«ã¯ã©ãããã°ããã§ããïŒãŸãã¯ãä»ã®äœããéžæããã¢ã€ãã ã®ã¿ã䜿çšããŠããªã¹ããããªãé·ãããããšãã§ããŸãããã®ãããªç¶æ³ã§ã¯ãå¹ççãªã¢ã¯ã»ã¹ã®ããã«ããŒã¿ã®èšæ¶ããã䜿çšãããŸããã¡ãªã¿ã«ãPythonã«é¢ãããã®ãããã¯ã«é¢ããèšäºãããã«èŠã€ããŸãã[5]ãåç §ããŠãã ããïŒãªãŒãããã³ã«é¢ããã¡ã¢åã®è©³çŽ°ã«ã€ããŠã¯ãèšäº[6]ãåç §ããŠãã ããïŒããããããã®å Žåã¯ã©ãã§ããïŒ
ãªã¹ããå®çŸ©ããããã®ãã®ãããªæ§æã®ä¿¡é Œæ§ãçããããããããŸãããæ¬åŒ§ã®ä»£ããã«è§æ¬åŒ§ã誀ã£ãŠäœ¿çšãããããã®éãè¡ã£ããããã®ã¯ç°¡åã§ããäžèŠçŸãããšã¬ã¬ã³ããªãœãªã¥ãŒã·ã§ã³ãå®éã«ã¯ç¹å®ã®åé¡ã«ã€ãªããå¯èœæ§ãããããšãå€æããŸãããããã°ã©ãã³ã°èšèªã¯ãæè¡çã§æè»æ§ããããäžæ¬æãªééããé²ãå¿ èŠããããŸãã
ã¡ãªã¿ã«ããªã¹ããšãžã§ãã¬ãŒã¿ãŒã®é·æãšçæã«ã€ããŠã®ãããã¯ã«ã€ããŠã¯ãäžèšã®çºèšãšäº€å·®ããŠãOlegMolchanovã«ããå¥ã®ãããªãèŠãããšãã§ããŸã[7]ã
3.ãžã§ãã¬ãŒã¿ãŒ-ã³ã«ã«ãŒãã³
Oleg Molchanov ã«ãã次ã®ãããª[8]ã¯ãã³ããã³ã®äœæ¥ã調æŽããããã®ãžã§ãã¬ãŒã¿ãŒã®äœ¿çšã«ã€ããŠèª¬æããŠããŸããå®éããããã¯ãããç®çãšããŠããŸããã³ã«ãã³ãåãæ¿ããç¬éã®éžæã«æ³šæãåããããŸãããããã®é 眮ã¯åçŽãªã«ãŒã«ã«åŸããŸã-ããããã³ã°é¢æ°ã®åã«yieldã¹ããŒãã¡ã³ãã眮ããŸããåŸè ã¯é¢æ°ãšããŠç解ãããä»ã®æäœãšæ¯èŒããŠæ»ãæéãéåžžã«é·ããããèšç®ã¯ãããã®åæ¢ã«é¢é£ä»ããããŸãããã®ããããããã¯ãããã«ãŒãšåŒã°ããŠããŸããã
åãæ¿ãã¯ãäžæãããããã»ã¹ããããããã³ã°ã³ãŒã«ãåŸ æ©ããªããšãã«æ£ç¢ºã«äœæ¥ãç¶è¡ããå Žåã«æå¹ã§ãããäœæ¥ã¯ããã«å®äºããŸãããããŠããã®ããã«ããã®ãã¹ãŠã®ãéšããã¯ãcoroutine / coroutineã¢ãã«ãäžå¿ã«éå§ãããããã§ããããããã£ãŠãéåæããã°ã©ãã³ã°ã®éçºã«åŒŸã¿ãã€ããããŸããããã ãã䞊åã³ã³ãã¥ãŒãã£ã³ã°ã®ä»®æ³ã¢ãã«ãäœæãããšãããcoroutinesã®å ã®ã¢ã€ãã¢ã¯ãŸã ç°ãªã£ãŠããŸããã
æ€èšäžã®ãããªã§ã¯ãã³ããã³ã®äžè¬çãªå Žåãšåæ§ã«ãã³ã«ãã³æäœã®ç¶ç¶ã¯ãã€ãã³ãã¹ã±ãžã¥ãŒã©ã§ããå€éšç°å¢ã«ãã£ãŠæ±ºå®ãããŸãããã®å Žåãevent_loopãšããååã®é¢æ°ã§è¡šãããŸãããããŠããã¹ãŠãè«ççã§ããããã«æãããŸããã¹ã±ãžã¥ãŒã©ãŒã¯åæãå®è¡ããå¿ èŠãªãšãã«æ£ç¢ºã«nextïŒïŒæŒç®åãåŒã³åºãããšã«ãã£ãŠãã«ãŒãã³ã®äœæ¥ãç¶è¡ããŸããåé¡ã¯ãäºæããªãåŸ æ©ã«ãããŸããã¹ã±ãžã¥ãŒã©ãŒã¯éåžžã«è€éã«ãªãå¯èœæ§ããããŸãã Molchanovã®ä»¥åã®ãããªïŒ[3]ãåç §ïŒã§ã¯ããã¹ãŠãåçŽã§ãããå¶åŸ¡ã®åçŽãªä»£æ¿è»¢éãå®è¡ãããŸããããããã¯ã¯ãããŸããã§ããã察å¿ããåŒã³åºãã¯ãããŸããã§ãããããã§ãããããã«ãããå°ãªããšãåçŽãªã¹ã±ãžã¥ãŒã©ãŒãå¿ èŠã§ããããšã匷調ããŸãã
åé¡1ã , next() (. event_loop). , , yield. - , , next(), .
2. , select, â . .
ããããéèŠãªã®ã¯ãã©ã³ããŒã®å¿ èŠæ§ã§ãããããŸãããã圌ã圌ã«ãšã£ãŠçããæ©èœãåŒãåãããšããäºå®ã§ããå€ãã®ã³ããã³ã®å ±åæäœã®ããã®ã¢ã«ãŽãªãºã ãå®è£ ããå¿ èŠããããšããäºå®ã«ãã£ãŠãç¶æ³ã¯ããã«è€éã«ãªããŸããOleg Molchanovãèšåãã2ã€ã®ãããªã§èª¬æãããŠããã¹ã±ãžã¥ãŒã©ãŒã®æ¯èŒã¯ãåæ§ã®åé¡ãéåžžã«æ確ã«åæ ããŠããŸãã[8]ã®ãœã±ããã¹ã±ãžã¥ãŒãªã³ã°ã¢ã«ãŽãªãºã ã¯ã[3]ã®ãã«ã«ãŒã»ã«ãã¢ã«ãŽãªãºã ãããèããè€éã§ãã
3.ã³ã«ãã£ã³ã®ãªãäžçãž
ãªãŒãããã³ã§å¯Ÿæãããã³ã«ãã£ã³ã®ãªãäžçãå¯èœã§ãããšç¢ºä¿¡ããŠããã®ã§ãåæ§ã®ã¿ã¹ã¯ããã§ã«ãããã«ãã£ãŠã©ã®ããã«è§£æ±ºãããŠãããã瀺ãå¿ èŠããããŸãããœã±ãããæäœããåãäŸã䜿çšããŠãããã瀺ããŸãããããã®æåã®å®è£ ã¯ãããã«ç解ã§ããã»ã©ç°¡åã§ã¯ãªãããšãå€æããããšã«æ³šæããŠãã ãããããã¯ããããªã®äœè èªèº«ã«ãã£ãŠç¹°ãè¿ã匷調ãããŠããŸããä»ã®äººã¯ãã³ã«ãã£ã³ã®æèã§åæ§ã®åé¡ã«çŽé¢ããŠããŸãããããã£ãŠãèªèãç解ããããã°ãªã©ã®è€éãã«é¢é£ããã³ããã³ã®æ¬ ç¹ããããª[10]ã§èª¬æãããŠããŸãã
ãŸããæ€èšäžã®ã¢ã«ãŽãªãºã ã®è€éãã«ã€ããŠç°¡åã«èª¬æããŸããããã¯ãã«ã¹ã¿ããŒãµãŒãã¹ããã»ã¹ã®åçã§è€æ°ã®æ§è³ªã«ãããã®ã§ãããããè¡ãããã«ãç¹å®ã®ããŒãããªãã¹ã³ãããµãŒããŒãäœæãããèŠæ±ã衚瀺ããããšãããã«å°éããå€ãã®ã¯ã©ã€ã¢ã³ããµãŒãã¹é¢æ°ãçæãããŸããå€ãã®ã¯ã©ã€ã¢ã³ããååšããå¯èœæ§ãããããããããã¯äºæãã衚瀺ãããŸããåçãªã¹ãã¯ããœã±ããã«ãµãŒãã¹ãæäŸããããããšæ å ±ã亀æããããã»ã¹ããäœæãããŸãããããª[8]ã§èª¬æãããŠããPythonãžã§ãã¬ãŒã¿ãœãªã¥ãŒã·ã§ã³ã®ã³ãŒãããªã¹ã1ã«ç€ºããŸãã
ãªã¹ã1.ãžã§ãã¬ãŒã¿ãŒã®ãœã±ãã
import socket
from select import select
tasks = []
to_read = {}
to_write = {}
def server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('localhost', 5001))
server_socket.listen()
while True:
yield ('read', server_socket)
client_socket, addr = server_socket.accept()
print('Connection from', addr)
tasks.append(client(client_socket, addr))
print('exit server')
def client(client_socket, addr):
while True:
yield ('read', client_socket)
request = client_socket.recv(4096)
if not request:
break
else:
response = 'Hello World\n'.encode()
yield ('write', client_socket)
client_socket.send(response)
client_socket.close()
print('Stop client', addr)
def event_loop():
while any([tasks, to_read, to_write]):
while not tasks:
ready_to_read, ready_to_write, _ = select(to_read, to_write, [])
for sock in ready_to_read:
tasks.append(to_read.pop(sock))
for sock in ready_to_write:
tasks.append(to_write.pop(sock))
try:
task = tasks.pop(0)
reason, sock = next(task)
if reason == 'read':
to_read[sock] = task
if reason == 'write':
to_write[sock] = task
except StopIteration:
print('Done!')
tasks.append(server())
event_loop()
ãµãŒããŒãšã¯ã©ã€ã¢ã³ãã®ã¢ã«ãŽãªãºã ã¯ããªãåºæ¬çã§ãããããããµãŒããŒãã¯ã©ã€ã¢ã³ãæ©èœãã¿ã¹ã¯ãªã¹ãã«è¿œå ããããšã¯ææ ®ãã¹ãããšã§ããããã«-詳现ïŒevent_loopã€ãã³ãã«ãŒãã®ã¢ã«ãŽãªãºã ãç解ããããšã¯å°é£ã§ããå°ãªããšããµãŒããŒããã»ã¹ãåžžã«ååšããå¿ èŠãããå Žåãã¿ã¹ã¯ãªã¹ãã空ã«ãªããŸã§ã¯ïŒ..
次ã«ãèŸæžto_readãšto_writeãå°å ¥ãããŸããèŸæžãæ±ãäœæ¥ãã®ãã®ã«ã¯ãå¥ã®èª¬æãå¿ èŠã§ããéåžžã®ãªã¹ãã§äœæ¥ãããããé£ããã§ãããã®ãããyieldã¹ããŒãã¡ã³ãã«ãã£ãŠè¿ãããæ å ±ã¯ãããã«åãããŠèª¿æŽãããŸãããããšãèŸæžã®åšãã§ãã¿ã³ããªã³ãšèžãããå§ãŸãããã¹ãŠãäžçš®ã®ãã·ãŒãºãã®ããã«ãªããŸããäœããèŸæžã«é 眮ãããŠããããã«èŠããããããã¿ã¹ã¯ãªã¹ãã«å ¥ããªã©ã§ããçããªãã¯ãé ãå£ããããšãã§ããããããã¹ãŠãæŽçããŸãã
ãããŠãç®åã®ã¿ã¹ã¯ã®è§£æ±ºçã¯ã©ã®ããã«ãªããŸããïŒãªãŒãããã³ãããããªã§ãã§ã«èª¬æãããœã±ãããæäœããããã»ã¹ãšåçã®ã¢ãã«ãäœæããããšã¯è«ççã§ãããµãŒããŒã¢ãã«ã§ã¯ãäœãå€æŽããå¿ èŠããªãããã«èŠããŸããããã¯ãserverïŒïŒé¢æ°ã®ããã«æ©èœãããªãŒãããã³ã«ãªããŸãããã®ã°ã©ããå³1ã«ç€ºããŸãã 1aããªãŒãããã³ã¢ã¯ã·ã§ã³y1ïŒïŒã¯ããµãŒããŒãœã±ãããäœæããæå®ãããããŒãã«æ¥ç¶ããŸããè¿°èªx1ïŒïŒã¯ã¯ã©ã€ã¢ã³ãæ¥ç¶ãå®çŸ©ããååšããå Žåãy2ïŒïŒã¢ã¯ã·ã§ã³ã¯ã¯ã©ã€ã¢ã³ããœã±ãããµãŒãã¹ããã»ã¹ãäœæããåŸè ãã¢ã¯ãã£ããªããžã§ã¯ãã¯ã©ã¹ãå«ãã¯ã©ã¹ããã»ã¹ãªã¹ãã«é 眮ããŸãã
å³ã§ã¯å³ïŒïœã¯ãåã ã®ã¯ã©ã€ã¢ã³ãã®ã¢ãã«ã®ã°ã©ãã瀺ããŠããããªãŒãããã³ã¯ç¶æ ã0ãã«ãããããã¯ã©ã€ã¢ã³ããæ å ±ãéä¿¡ããæºåãã§ããŠãããã©ãããå€æãïŒè¿°èªx1ïŒïŒ-trueïŒãç¶æ ã1ããžã®é·ç§»æã«ã¢ã¯ã·ã§ã³y1ïŒïŒå ã§å¿çãåä¿¡ããŸããããã«ãã¯ã©ã€ã¢ã³ããæ å ±ãåä¿¡ããæºåãã§ããŠããå ŽåïŒx2ïŒïŒã¯ãã§ã«trueã§ããå¿ èŠããããŸãïŒãã¢ã¯ã·ã§ã³y2ïŒïŒã¯ãåæç¶æ ã0ããžã®é·ç§»æã«ã¯ã©ã€ã¢ã³ãã«ã¡ãã»ãŒãžãéä¿¡ããæäœãå®è£ ããŸããã¯ã©ã€ã¢ã³ãããµãŒããŒãšã®æ¥ç¶ãåæãããšïŒãã®å Žåãx3ïŒïŒã¯falseïŒããªãŒãããã³ã¯ç¶æ ã4ãã«åãæ¿ãããy3ïŒïŒã¢ã¯ã·ã§ã³ã§ã¯ã©ã€ã¢ã³ããœã±ãããéããŸããããã»ã¹ã¯ãã¢ã¯ãã£ããªã¯ã©ã¹ã®ãªã¹ãããé€å€ããããŸã§ã4ãç¶æ ã®ãŸãŸã§ãïŒãªã¹ãã®åœ¢æã«ã€ããŠã¯ããµãŒããŒã¢ãã«ã®äžèšã®èª¬æãåç §ããŠãã ããïŒã
å³ã§ã¯1cã¯ããªã¹ã1ã®event_loopïŒïŒé¢æ°ãšåæ§ã®ããã»ã¹ã®èµ·åãå®è£ ãããªãŒãããã³ã瀺ããŠããŸãããã®å Žåã®ã¿ããã®æäœã¢ã«ãŽãªãºã ã¯ã¯ããã«åçŽã§ããããã¯ãã¹ãŠããã·ã³ãã¢ã¯ãã£ããªã¯ã©ã¹ã®ãªã¹ãã®èŠçŽ ã調ã¹ãŠããããã®ããããã«å¯ŸããŠloopïŒïŒã¡ãœãããåŒã³åºããšããäºå®ã«åž°çããŸãããã®ã¢ã¯ã·ã§ã³ã¯y2ïŒïŒã«ãã£ãŠå®è£ ãããŸãã y4ïŒïŒã¢ã¯ã·ã§ã³ã¯ãç¶æ ã4ãã«ããã¯ã©ã¹ããªã¹ãããé€å€ããŸããæ®ãã®ã¢ã¯ã·ã§ã³ã¯ããªããžã§ã¯ãã®ãªã¹ãã®ã€ã³ããã¯ã¹ã§æ©èœããŸããy3ïŒïŒã¢ã¯ã·ã§ã³ã¯ã€ã³ããã¯ã¹ãå¢ãããy1ïŒïŒã¢ã¯ã·ã§ã³ã¯ã€ã³ããã¯ã¹ããªã»ããããŸãã
Pythonã®ãªããžã§ã¯ãããã°ã©ãã³ã°æ©èœã¯ãC ++ã®ãªããžã§ã¯ãããã°ã©ãã³ã°ãšã¯ç°ãªããŸãããããã£ãŠããªãŒãããã³ã¢ãã«ã®æãåçŽãªå®è£ ãåºç€ãšããŠäœ¿çšãããŸãïŒæ£ç¢ºã«ã¯ããªãŒãããã³ã®æš¡å£ã§ãïŒãããã¯ãããã»ã¹ãè¡šããšãããªããžã§ã¯ãã®ååã«åºã¥ããŠãããåããã»ã¹ã¯åå¥ã®ã¢ã¯ãã£ãã¯ã©ã¹ã«å¯Ÿå¿ããŸãïŒãšãŒãžã§ã³ããšãåŒã°ããŸãïŒããã®ã¯ã©ã¹ã«ã¯ãå¿ èŠãªããããã£ãšã¡ãœãããå«ãŸãïŒç¹å®ã®ãªãŒãããã³ã¡ãœããã®è©³çŽ°- [9]ã®è¿°èªãšã¢ã¯ã·ã§ã³ãåç §ïŒããªãŒãããã³æäœã®ããžãã¯ïŒé·ç§»é¢æ°ãšçµäºé¢æ°ïŒã¯ãloopïŒïŒãšåŒã°ããã¡ãœããã®ãã¬ãŒã ã¯ãŒã¯å ã«éäžããŠããŸãããªãŒãããã³ã®åäœã®ããžãã¯ãå®è£ ããããã«ãif-elif-elseæ§é ã䜿çšããŸãã
ãã®ã¢ãããŒãã§ã¯ããã€ãã³ãã«ãŒããã¯ãœã±ããã®å¯çšæ§ã®åæãšã¯äœã®é¢ä¿ããããŸããããããã¯ãè¿°èªå ã§åãselectã¹ããŒãã¡ã³ãã䜿çšããããã»ã¹èªäœã«ãã£ãŠãã§ãã¯ãããŸãããã®ç¶æ³ã§ã¯ããããã¯ãªã¹ãã§ã¯ãªãåäžã®ãœã±ããã§åäœãããã®ç¹å®ã®ãœã±ããã«æåŸ ãããåäœããã§ãã¯ããæ£ç¢ºã«ã¯åäœã¢ã«ãŽãªãºã ã«ãã£ãŠæ±ºå®ãããç¶æ³ã§åäœããŸããã¡ãªã¿ã«ããã®ãããªå®è£ ããããã°ããéçšã§ãselectã¹ããŒãã¡ã³ãã®äºæãã¬ããããã³ã°ãšãã»ã³ã¹ãçŸããŸããã
å³ïŒ1.ãœã±ãããæäœããããã®èªåããã»ã¹ã®ã°ã©ã
ãªã¹ã2ã¯ããœã±ãããæäœããããã®Pythonã®ãªãŒãããã³ãªããžã§ã¯ãã³ãŒãã瀺ããŠããŸããããã¯ãç§ãã¡ã®äžçš®ã®ãã³ããã³ã®ãªãäžçãã§ããããã¯ããœãããŠã§ã¢ããã»ã¹ãèšèšããããã®ããŸããŸãªååãæã€ãäžçãã§ããããã¯ã䞊åèšç®ã®ã¢ã«ãŽãªãºã ã¢ãã«ã®ååšãç¹åŸŽãšããŠããŸãïŒè©³çŽ°ã«ã€ããŠã¯ã[9]ãåç §ããŠãã ãããããã¯ããªãŒãããã³ããã°ã©ãã³ã°ãã¯ãããžïŒAPïŒãšãéåžžã®ãã¯ãããžãã®äž»ãªå®æ§çãªéãã§ãã
Automatonããã°ã©ãã³ã°ã¯ãããã°ã©ã èšèšãããã»ã¹ã®äžŠååŠçãããã³åæã«ããã°ã©ããŒã®å¿ãèããããšãã§ãããã¹ãŠã®éåæååãç°¡åã«å®è£ ããŸããç§ã®ä»¥åã®èšäºã§ã¯ãèªåèšç®ã®æ§é ã¢ãã«ã®èª¬æãšãã®ã¢ããªã±ãŒã·ã§ã³ã®äŸãžã®æ£åŒãªå®çŸ©ããå§ããŠãããããã詳现ã«èª¬æããŠããŸããäžèšã®Pythonã®ã³ãŒãã¯ãã³ã«ãŒãã³ã®ã³ã«ãŒãã³ååã®èªåå®è£ ã瀺ããŠãããããããå®å šã«ãªãŒããŒã©ãããããã¹ããŒããã·ã³ã¢ãã«ã§è£è¶³ããã³æ¡åŒµããŠããŸãã
ãªã¹ã2.ãã·ã³äžã®ãœã±ãã
import socket
from select import select
timeout = 0.0; classes = []
class Server:
def __init__(self): self.nState = 0;
def x1(self):
self.ready_client, _, _ = select([self.server_socket], [self.server_socket], [], timeout)
return self.ready_client
def y1(self):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind(('localhost', 5001))
self.server_socket.listen()
def y2(self):
self.client_socket, self.addr = self.server_socket.accept()
print('Connection from', self.addr)
classes.append(Client(self.client_socket, self.addr))
def loop(self):
if (self.nState == 0): self.y1(); self.nState = 1
elif (self.nState == 1):
if (self.x1()): self.y2(); self.nState = 0
class Client:
def __init__(self, soc, adr): self.client_socket = soc; self.addr = adr; self.nState = 0
def x1(self):
self.ready_client, _, _ = select([self.client_socket], [], [], timeout)
return self.ready_client
def x2(self):
_, self.write_client, _ = select([], [self.client_socket], [], timeout)
return self.write_client
def x3(self): return self.request
def y1(self): self.request = self.client_socket.recv(4096);
def y2(self): self.response = 'Hello World\n'.encode(); self.client_socket.send(self.response)
def y3(self): self.client_socket.close(); print('close Client', self.addr)
def loop(self):
if (self.nState == 0):
if (self.x1()): self.y1(); self.nState = 1
elif (self.nState == 1):
if (not self.x3()): self.y3(); self.nState = 4
elif (self.x2() and self.x3()): self.y2(); self.nState = 0
class EventLoop:
def __init__(self): self.nState = 0; self.i = 0
def x1(self): return self.i < len(classes)
def y1(self): self.i = 0
def y2(self): classes[self.i].loop()
def y3(self): self.i += 1
def y4(self):
if (classes[self.i].nState == 4):
classes.pop(self.i)
self.i -= self.i
def loop(self):
if (self.nState == 0):
if (not self.x1()): self.y1();
if (self.x1()): self.y2(); self.y4(); self.y3();
namSrv = Server(); namEv = EventLoop()
while True:
namSrv.loop(); namEv.loop()
ãªã¹ã2ã®ã³ãŒãã¯ããªã¹ã1ã®ã³ãŒããããã¯ããã«æè¡çã«é²ãã§ããŸããããããèªåèšç®ã¢ãã«ã®ã¡ãªããã§ããããã¯ããªãŒãããã³ã®åäœãããã°ã©ãã³ã°ãªããžã§ã¯ãã¢ãã«ã«çµ±åããããšã§å®¹æã«ãªããŸãããã®çµæããªãŒãããã³ããã»ã¹ã®åäœã®ããžãã¯ã¯ãçæãããå Žæã«æ£ç¢ºã«éäžããéåžžã®ããã«ããã»ã¹å¶åŸ¡ã®ã€ãã³ãã«ãŒãã«å§ä»»ãããããšã¯ãããŸãããæ°ãããœãªã¥ãŒã·ã§ã³ã¯ããŠãããŒãµã«ãªãã€ãã³ãã«ãŒããã®äœæãåŒãèµ·ãããŸãããã®ãããã¿ã€ãã¯ãEventLoopã¯ã©ã¹ã®ã³ãŒããšèŠãªãããšãã§ããŸãã
4.SRPããã³DRYã®ååã«ã€ããŠ
ãåäžè²¬ä»»ãã®åå-SRPïŒåäžè²¬ä»»ååïŒããã³ãèªåèªèº«ãç¹°ãè¿ããªãã-DRYïŒèªåèªèº«ãç¹°ãè¿ããªãïŒã¯ãOlegMolchanovã«ããå¥ã®ãããªã®ã³ã³ããã¹ãã§è¡šæãããŠããŸã[11]ã圌ãã«ãããšãé¢æ°ã«ã¯ãSRYã®ååã«éåããªãããã«ã¿ãŒã²ããã³ãŒãã®ã¿ãå«ãŸããDRYã®ååã«éåããªãããã«ãè¿œå ã³ãŒããã®ç¹°ãè¿ãã«å¯äžããªãããã«ããå¿ èŠããããŸãããã®ç®çã®ããã«ããã³ã¬ãŒã¿ã䜿çšããããšãææ¡ãããŠããŸããããããå¥ã®è§£æ±ºçããããŸã-èªåã§ãã
ååã®èšäº[2]ãã®ãããªååã®ååšã«æ°ã¥ããã«ããã³ã¬ãŒã¿ã䜿çšããŠäŸã瀺ãããŸãããã¡ãªã¿ã«ãå¿ èŠã«å¿ããŠãªã¹ããçæã§ããã«ãŠã³ã¿ãŒãæ€èšããŸãããã«ãŠã³ã¿ãŒã®çšŒåæéã枬å®ããã¹ããããŠã©ãããªããžã§ã¯ãã«ã€ããŠèª¬æããŸãããªããžã§ã¯ããSRPããã³DRYã®ååã«æºæ ããŠããå Žåããã®æ©èœã¯éä¿¡ãããã³ã«ã»ã©éèŠã§ã¯ãããŸãããå®è£ ã§ã¯ãã«ãŠã³ã¿ãŒã³ãŒãã¯ã¹ããããŠã©ããã³ãŒããšã¯é¢ä¿ããªãããªããžã§ã¯ãã®ãããããå€æŽããŠãä»ã®ãªããžã§ã¯ãã«ã¯åœ±é¿ããŸããããããã¯ãããã³ã«ã«ãã£ãŠã®ã¿ææããããªããžã§ã¯ãã¯ã海岞ã§ãåæããå³å¯ã«ããã«åŸããŸãã
ãããã£ãŠã䞊åãªãŒãããã³ã¢ãã«ã¯ãåºæ¬çã«ãã³ã¬ãŒã¿ã®æ©èœãäžæžãããŸãããããã®æ©èœãå®è£ ããæ¹ãããæè»ã§ç°¡åã§ããæ©èœã³ãŒãããå²ã¿ããŸããïŒè£ 食ããŸããïŒããªãŒãããã³ãšåŸæ¥ã®æè¡ã®å®¢èŠ³çãªè©äŸ¡ãšæ¯èŒãç®çãšããŠããªã¹ã3ã¯ãåã®èšäº[2]ã§èª¬æããã«ãŠã³ã¿ãŒã®ãªããžã§ã¯ãã¢ããã°ã瀺ããŠããŸããããã§ã¯ãå®è¡æå»ãå«ãç°¡ç¥åãããããŒãžã§ã³ãšãã«ãŠã³ã¿ãŒã®å ã®ããŒãžã§ã³ãã³ã¡ã³ãã®åŸã«ç€ºãããŠããŸãã
ãªã¹ã3.èªåã«ãŠã³ã¿ãŒã®å®è£
import time
# 1) 110.66 sec
class PCount:
def __init__(self, cnt ): self.n = cnt; self.nState = 0
def x1(self): return self.n > 0
def y1(self): self.n -=1
def loop(self):
if (self.nState == 0 and self.x1()):
self.y1();
elif (self.nState == 0 and not self.x1()): self.nState = 4;
class PTimer:
def __init__(self, p_count):
self.st_time = time.time(); self.nState = 0; self.p_count = p_count
# def x1(self): return self.p_count.nStat == 4 or self.p_count.nState == 4
def x1(self): return self.p_count.nState == 4
def y1(self):
t = time.time() - self.st_time
print ("speed CPU------%s---" % t)
def loop(self):
if (self.nState == 0 and self.x1()): self.y1(); self.nState = 1
elif (self.nState == 1): pass
cnt1 = PCount(1000000)
cnt2 = PCount(10000)
tmr1 = PTimer(cnt1)
tmr2 = PTimer(cnt2)
# event loop
while True:
cnt1.loop(); tmr1.loop()
cnt2.loop(); tmr2.loop()
# # 2) 73.38 sec
# class PCount:
# def __init__(self, cnt ): self.n = cnt; self.nState = 0
# def loop(self):
# if (self.nState == 0 and self.n > 0): self.n -= 1;
# elif (self.nState == 0 and not self.n > 0): self.nState = 4;
#
# class PTimer:
# def __init__(self): self.st_time = time.time(); self.nState = 0
# def loop(self):
# if (self.nState == 0 and cnt.nState == 4):
# t = time.time() - self.st_time
# print("speed CPU------%s---" % t)
# self.nState = 1
# elif (self.nState == 1): exit()
#
# cnt = PCount(100000000)
# tmr = PTimer()
# while True:
# cnt.loop();
# tmr.loop()
# # 3) 35.14 sec
# class PCount:
# def __init__(self, cnt ): self.n = cnt; self.nState = 0
# def loop(self):
# if (self.nState == 0 and self.n > 0):
# self.n -= 1;
# return True
# elif (self.nState == 0 and not self.n > 0): return False;
#
# cnt = PCount(100000000)
# st_time = time.time()
# while cnt.loop():
# pass
# t = time.time() - st_time
# print("speed CPU------%s---" % t)
# # 4) 30.53 sec
# class PCount:
# def __init__(self, cnt ): self.n = cnt; self.nState = 0
# def loop(self):
# while self.n > 0:
# self.n -= 1;
# return True
# return False
#
# cnt = PCount(100000000)
# st_time = time.time()
# while cnt.loop():
# pass
# t = time.time() - st_time
# print("speed CPU------%s---" % t)
# # 5) 18.27 sec
# class PCount:
# def __init__(self, cnt ): self.n = cnt; self.nState = 0
# def loop(self):
# while self.n > 0:
# self.n -= 1;
# return False
#
# cnt = PCount(100000000)
# st_time = time.time()
# while cnt.loop():
# pass
# t = time.time() - st_time
# print("speed CPU------%s---" % t)
# # 6) 6.96 sec
# def count(n):
# st_time = time.time()
# while n > 0:
# n -= 1
# t = time.time() - st_time
# print("speed CPU------%s---" % t)
# return t
#
# def TestTime(fn, n):
# def wrapper(*args):
# tsum=0
# st = time.time()
# i=1
# while (i<=n):
# t = fn(*args)
# tsum +=t
# i +=1
# return tsum
# return wrapper
#
# test1 = TestTime(count, 2)
# tt = test1(100000000)
# print("Total ---%s seconds ---" % tt)
ããŸããŸãªãªãã·ã§ã³ã®çšŒåæéãè¡šã«ãŸãšããäœæ¥çµæã«ã€ããŠã³ã¡ã³ãããŸãããã
- ã¯ã©ã·ãã¯ãªãŒãããã³ã®å®è£ -110.66ç§
- ãªãŒãããã³ã¡ãœãããªãã®ãªãŒãããã³å®è£ -73.38ç§
- èªåã¹ããããŠã©ãããªã-35.14
- åå埩ã§åºåãããŠããéã®ãã©ãŒã ã®ã«ãŠã³ã¿ãŒ-30.53
- ããããã³ã°ãµã€ã¯ã«ã®ããã«ãŠã³ã¿ãŒ-18.27
- ãã³ã¬ãŒã¿ä»ãã®ãªãªãžãã«ã«ãŠã³ã¿ãŒ-6.96
èªåã«ãŠã³ã¿ãŒã¢ãã«ãå®å šã«è¡šãæåã®ãªãã·ã§ã³ãã«ãŠã³ã¿ãŒèªäœãšã¹ããããŠã©ããã®å®è¡æéãæãé·ããªããŸããèªåæè¡ã®åçããããããããšã§ã皌åæéãççž®ããããšãã§ããŸããããã«åŸã£ãŠããªãã·ã§ã³2ã§ã¯ãè¿°èªãšã¢ã¯ã·ã§ã³ã®åŒã³åºãããããã®ã³ãŒãã«çœ®ãæããããŸãããã®ããã«ããŠãã¡ãœããåŒã³åºãæŒç®åã®æéãç¯çŽããŸãããããã¯éåžžã«ç®ç«ã¡ãŸãã30ç§ä»¥äžãåäœæéãççž®ããŸããã
3çªç®ã®ãªãã·ã§ã³ã§ããå°ãç¯çŽããããåçŽãªã«ãŠã³ã¿ãŒå®è£ ãäœæããŸããããã«ãŠã³ã¿ãŒãµã€ã¯ã«ã®åå埩ã§çµäºããŸããïŒéåžžã®æäœã®æš¡å£ïŒãã«ãŠã³ã¿ãŒã®åæ¢ããªããããšã§ïŒãªãã·ã§ã³5åç §ïŒãã«ãŠã³ã¿ãŒã®äœæ¥ãå€§å¹ ã«åæžããŸããããããåæã«ãç§ãã¡ã¯éåžžã®ä»äºã®å©ç¹ã倱ããŸããããªãã·ã§ã³6-ããã¯ããã³ã¬ãŒã¿ããã§ã«ç¹°ãè¿ãããŠããå ã®ã«ãŠã³ã¿ãŒã§ãããå®è¡æéãæå°ã§ãããã ãããªãã·ã§ã³5ãšåæ§ã«ãããã¯ããããã³ã°å®è£ ã§ãããé¢æ°ã®éåžžã®æäœã«ã€ããŠèª¬æããå Žåã«ã¯é©ããŠããŸããã
5ãçµè«
ãªãŒãããã³ãã¯ãããžãŒã䜿çšããããä¿¡é Œã§ããã«ãŒãã³ã䜿çšãããã¯ãå®å šã«ããã°ã©ããŒã«æ±ºå®ãããŸããããã§ç§ãã¡ã«ãšã£ãŠéèŠãªã®ã¯ãããã°ã©ã èšèšã«ã¯ãéåžžã®ã¢ãããŒããšã¯ç°ãªãã¢ãããŒã/ãã¯ãããžãŒãããããšã圌ãç¥ã£ãŠããããšã§ãã次ã®ãšããŸããã¯ãªãªãã·ã§ã³ãæ³åããããšãã§ããŸãããŸããã¢ãã«èšèšæ®µéã§ããªãŒãããã³ãœãªã¥ãŒã·ã§ã³ã¢ãã«ãäœæãããŸããããã¯å³å¯ã«ç§åŠçã§ã蚌æ ã«åºã¥ããŠãããååã«ææžåãããŠããŸãã次ã«ãããšãã°ãããã©ãŒãã³ã¹ãåäžãããããã«ããªã¹ã3ã«ç€ºãããã«ãã³ãŒãã®ãéåžžã®ãããŒãžã§ã³ã«ãå€åœ¢ããããŸããã³ãŒãã®ãéãªãã¡ã¯ã¿ãªã³ã°ããæ³åããããšãã§ããŸãã 7çªç®ã®ãªãã·ã§ã³ãã1çªç®ã®ãªãã·ã§ã³ãžã®ç§»è¡ã§ãããããã¯å¯èœã§ãããã€ãã³ãã®å¯èœæ§ãæãäœãã³ãŒã¹ã§ã:)
å³ã 2ã¯ããããã¯ãéåæãã«é¢ãããããªã®ã¹ã©ã€ãã瀺ããŠããŸã[10]ã..ããããŠããæªããã¯ãè¯ãããäžåã£ãŠããããã§ãããããŠãç§ã®æèŠã§ã¯ããªãŒãããã³ãåžžã«åªããŠããå Žåãéåæããã°ã©ãã³ã°ã®å Žåã¯ã圌ããèšãããã«ãããªãã®å¥œã¿ã«åãããŠéžæããŠãã ããããããããæªãããªãã·ã§ã³ãæãå¯èœæ§ãé«ãããã§ãããŸããããã°ã©ããŒã¯ãããã°ã©ã ãèšèšãããšãã«ãããã«ã€ããŠäºåã«ç¥ã£ãŠããå¿ èŠããããŸãã
å³ïŒ2.éåæããã°ã©ãã³ã°ã®ç¹åŸŽ
確ãã«ããªãŒãããã³ã³ãŒãã¯ããã眪ããªãããã§ã¯ãªãããã³ãŒãã®éãå°ãå€ããªããŸããããããæåã«ãããã¯ããããæ§é åãããŠããã®ã§ãç解ãããããç¶æããããã§ãããããŠã第äºã«ãããã¯åžžã«å€§ãããªããšã¯éããŸãããè€éããå¢ãã«ã€ããïŒããšãã°ããªãŒãããã³ã¡ãœããã®åå©çšã«ããïŒããã«å©çãåŸãããå¯èœæ§ããããŸãããããã°ãããç°¡åã§æ確ã«ãªããŸããã¯ããçµå±ã®ãšãããããã¯å®å šã«SRPãšDRYã§ãããããŠãããã¯æã ãã¯ããã«éèŠã§ãã
ããšãã°ãé¢æ°ã®èšèšã®æšæºã«æ³šæãæãããšãæãŸãããããããå¿ èŠã§ãããããŸããããã°ã©ããŒã¯ãå¯èœãªéããããããã³ã°é¢æ°ã®èšèšãé¿ããå¿ èŠããããŸãããããè¡ãã«ã¯ãèšç®ããã»ã¹ãéå§ããã ãã§ãå®å šæ§ããã§ãã¯ãããããäŸã§æ€èšãããŠããselecté¢æ°ã®ããã«éå§ã®æºåãã§ããŠãããã©ããããã§ãã¯ããæ段ãå¿ èŠã§ãããªã¹ã4ã«ç€ºãããã«ãDOSæ代ã«ããã®ãŒãé¢æ°ã䜿çšããã³ãŒãã¯ããã®ãããªåé¡ã«ã¯é·ããã«ãŒãã³åãã®å±¥æŽãããããšã瀺ããŠããŸãã
ãªã¹ã4.ããŒããŒãããæåãèªã¿åã
/*
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int C=0;
while (C != 'e')
{
C = getch();
putchar (C);
}
return a.exec();
}
*/
//*
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int C=0;
while (C != 'e')
{
if (kbhit()) {
C = getch();
putch(C);
}
}
return a.exec();
}
//*/
ããŒããŒãããæåãèªã¿åãããã®2ã€ã®ãªãã·ã§ã³ããããŸããæåã®ãªãã·ã§ã³ã¯ããããã³ã°ã§ããèšç®ããããã¯ããgetchïŒïŒé¢æ°ãããŒããŒãããæåãåãåããŸã§ãã¹ããŒãã¡ã³ããå®è¡ããŠæåãåºåããŸããã 2çªç®ã®ããªã¢ã³ãã§ã¯ããã¢ã®é¢æ°kbhitïŒïŒãæåãå ¥åãããã¡ãŒã«ããããšã確èªãããšãã«ãåãé¢æ°ãé©åãªã¿ã€ãã³ã°ã§ã®ã¿èµ·åãããŸãããããã£ãŠãèšç®ããããã¯ãããããšã¯ãããŸããã
é¢æ°èªäœããéããå Žåãã€ãŸãäœæ¥ã«ã¯ããªãã®æéãå¿ èŠã§ãããã³ã«ãã³ã®äœæ¥ã®çš®é¡ã«ãã£ãŠå®æçã«çµäºããïŒããã¯ãåãã³ã«ãã³ã®ã¡ã«ããºã ã䜿çšããã«å®è¡ã§ããããããã€ã³ããããªãããã«ããïŒããšã¯å°é£ã§ããããããŸãæå³ããããŸããããã®å Žåããã®ãããªæ©èœãå¥ã®ã¹ã¬ããã«é 眮ããŠãããäœæ¥ã®å®äºãå¶åŸ¡ããŸãïŒ[2]ã®QCountã¯ã©ã¹ã®å®è£ ãåç §ããŠãã ããïŒãïŒã
èšç®ã®ãããã¯ãé€å€ããæ¹æ³ããã€ã§ãèŠã€ããããšãã§ããŸããäžèšã§ã¯ãcoroutine / coroutineã¡ã«ããºã ããVKPïŒaïŒèªåããã°ã©ãã³ã°ç°å¢ãªã©ã®ç¹æ®ãªç°å¢ã䜿çšããã«ãèšèªã®éåžžã®æ段ã®ãã¬ãŒã ã¯ãŒã¯å ã§éåæã³ãŒããäœæããæ¹æ³ã瀺ããŸããããããŠãäœãã©ã®ããã«äœ¿çšãããã¯ãããã°ã©ããŒã決å®ããŸãã
æç®
1.Pythonãžã¥ãã¢ããããã£ã¹ãã pythonã®éåæã«ã€ããŠã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = Q2r76grtNegãç¡æãèšèªããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
2.åæå®è¡æ§ãšå¹çæ§ïŒPythonãšFSMã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒhabr.com/ru/post/506604ãç¡æãã€ãºããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
3. Molchanov O. Pythonã®éåæã®åºç€ïŒ4ïŒãžã§ãã¬ãŒã¿ãŒãšã©ãŠã³ãããã³ã€ãã³ãã«ãŒãã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = PjZUSSkGLE8 ]ãç¡æãèšèªããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
4.48ã®ãžã§ãã¬ãŒã¿ãŒãšã€ãã¬ãŒã¿ãŒã Pythonã®ãžã§ãã¬ãŒã¿åŒã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = vn6bV6BYm7wã èªç±ãã€ãºããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
5.ã¡ã¢åãšã«ã¬ãŒïŒPythonïŒã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒhabr.com/ru/post/335866ãç¡æãèšèªããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
6.ãªã¥ããã§ã³ã³V.S.ååž°ãžã®å¯ŸåŠã«ã€ããŠã ãPCã¯ãŒã«ãããNo.11 / 02ãwww.osp.ru/pcworld/2002/11/164417
7. Molchanov O. Python CastïŒ10-æ©çãŸããšã¯äœã§ããã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = ZjaVrzOkpZkãç¡æãèšèªããã·ã¢ïŒæ²»çæ¥2020幎7æ18æ¥ïŒã
8. Molchanov O. Pythonã§ã®éåæã®åºç€ïŒ5ïŒãžã§ãã¬ãŒã¿ãŒã§ã®éåæã [é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = hOP9bKeDOHsãç¡æãèšèªããã·ã¢ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
9.䞊åã³ã³ãã¥ãŒãã£ã³ã°ã¢ãã«ã[é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒhabr.com/ru/post/486622ãç¡æãã€ãºããã·ã¢ ïŒæ²»çæ¥2020幎7æ20æ¥ïŒã
10. Polishchuk A.Pythonã§ã®éåæã[é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = lIkA0TDX8tEãç¡æãã€ãºããã·ã¢ ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã
11. Molchanov O.ã¬ãã¹ã³Pythonãã£ã¹ãïŒ6-ãã³ã¬ãŒã¿ãŒã[é»åãªãœãŒã¹]ãã¢ã¯ã»ã¹ã¢ãŒãïŒwww.youtube.com/watchïŒv = Ss1M32pp5Ewãç¡æãèšèªããã·ã¢ ïŒæ²»çæ¥2020幎7æ13æ¥ïŒã