Swooleʵ¼ù£ºÔõÑùʹÓÃгÌÓÅ»¯Êý¾Ý¿â»á¼û
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬Êý¾Ý¿â»á¼û³ÉΪÁËÐí¶àwebÓ¦ÓõĻù´¡ÐèÇó¡£Ôڸ߲¢·¢ºÍ´óÁ÷Á¿µÄÇéÐÎÏ£¬¹Å°åµÄÊý¾Ý¿â»á¼û·½·¨ÍùÍù»á·ºÆðÆ¿¾±£¬µ¼ÖÂÐÔÄÜϽµ£¬ÉõÖÁϵͳÍ߽⡣¶øswoole×÷Ϊһ¿î»ùÓÚг̵ĸßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬¿ÉÒÔ×ÊÖúÎÒÃÇÓÅ»¯Êý¾Ý¿â»á¼û£¬Ìá¸ßÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSwooleµÄгÌÌØÕ÷À´ÓÅ»¯MySQLÊý¾Ý¿â»á¼û¡£
Ò»¡¢Ð³ÌµÄ»ù±¾¿´·¨ºÍÓÅÊÆ
гÌÊÇÒ»ÖÖÓû§Ì¬Ị̈߳¬Ò²³ÆÇáÁ¿¼¶Ï̡߳£Ëü²»ÐèÒª²Ù×÷ϵͳµÄÏ̵߳÷ÀíÆ÷À´¾ÙÐе÷Àí£¬ËùÓÐÓÉÓ¦ÓóÌÐò¿ØÖÆ¡£Ð³Ì¿ÉÒÔͨ¹ýyield²Ù×÷£¬´ÓÄ¿½ñÖ´ÐÐÁ÷³ÌÖÐÔÝÍ£²¢ÉúÑÄÆäÉÏÏÂÎÄ£¬ÈÃÁíÒ»¸öг̼ÌÐøÖ´ÐС£ÕâÑùµÄÇл»ÊÇÔÚÓû§Ì¬Ï¾ÙÐеģ¬±ÈÏ̵߳ÄÇл»¸ü¿ì£¬ÏûºÄ¸üÉÙµÄϵͳ×ÊÔ´¡£Ð³ÌµÄÓÅÊÆÔÚÓÚ£º¸ß²¢·¢¡¢µÍ¿ªÏú¡¢¸ßЧÂÊ¡£
¶þ¡¢SwooleÖеÄгÌ
SwooleÊÇÒ»¿î»ùÓÚг̵ĸßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬ÄÚÖÃÁËг̵÷ÀíÆ÷£¬ÔÚʵÏָ߲¢·¢ÍøÂçͨѶµÄͬʱ£¬Ò²Àû±ãÁËг̵ÄʹÓá£ÔÚSwooleÖУ¬½¨ÉèгÌÖ»ÐèҪʹÓÃÒªº¦×Ö¡°coroutine¡±¼´¿É¡£Ê¾Àý´úÂëÈçÏ£º
//½¨ÉèÐ³Ì go(function(){ echo "Hello, Coroutine!"; });
µÇ¼ºó¸´ÖÆ
Èý¡¢Ê¹ÓÃгÌÓÅ»¯Êý¾Ý¿â»á¼û
MySQLÊÇÒ»¿î¿ªÔ´µÄ¹ØϵÐÍÊý¾Ý¿â£¬ÆÕ±éÓ¦ÓÃÓÚWebÓ¦ÓÃÖС£Ôڸ߲¢·¢ºÍ´óÁ÷Á¿µÄÇéÐÎÏ£¬¹Å°åµÄMySQL»á¼û·½·¨¿ÉÄ᷺ܻÆðÐÔÄÜÆ¿¾±¡£Ê¹ÓÃSwooleµÄгÌÌØÕ÷£¬¿ÉÒÔÓÅ»¯MySQLµÄ»á¼û·½·¨£¬Ìá¸ßÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£
ʹÓÃÅþÁ¬³Ø
Ôڸ߲¢·¢ÇéÐÎÏ£¬ÆµÈÔ½¨ÉèºÍÏú»ÙMySQLÅþÁ¬»áÔì³ÉºÜ´óµÄ¿ªÏú¡£Ê¹ÓÃÅþÁ¬³Ø¿ÉÒÔïÔÌÅþÁ¬µÄ½¨ÉèºÍÏú»Ù£¬Ìá¸ßÊý¾Ý¿â»á¼ûµÄЧÂÊ¡£Ê¾Àý´úÂëÈçÏ£º
//½¨ÉèÅþÁ¬³Ø $pool = new SwooleCoroutineChannel(50); //г̳ØʹÃü function db_task($sql) { $conn = $pool->pop(); //´ÓÅþÁ¬³Ø»ñÈ¡ÅþÁ¬ $result = $conn->query($sql); //Ö´ÐÐSQLÓï¾ä $pool->push($conn); //ËÍ»¹ÅþÁ¬µ½ÅþÁ¬³Ø return $result; } //½¨ÉèÐ³Ì go(function () { $result = db_task("SELECT * FROM users WHERE id = 1"); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ʹÓÃгÌMySQL¿Í»§¶Ë
SwooleÌṩÁËгÌMySQL¿Í»§¶Ë£¬¿ÉÒÔÖ±½ÓʹÓÃгÌÀ´¾ÙÐÐMySQLµÄ²Ù×÷£¬¼òÆÓ¸ßЧ¡£Ê¾Àý´úÂëÈçÏ£º
//½¨ÉèMySQL¿Í»§¶Ë $pool = new SwooleCoroutineMySQL(); $pool->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => '123456', 'database' => 'test', ]); //гÌMySQLʹÃü function db_task($sql) { global $pool; $result = $pool->query($sql); return $result; } //½¨ÉèÐ³Ì go(function () { $result = db_task("SELECT * FROM users WHERE id = 1"); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ÅúÁ¿´¦Öóͷ£SQLÓï¾ä
ÔÚ»á¼ûÊý¾Ý¿âʱ£¬ÍùÍùÐèÒªÖ´ÐжàÌõSQLÓï¾ä¡£¹Å°åµÄ·½·¨ÊÇÖðÌõÖ´ÐУ¬ÕâÑù»áÔì³É´ó×ÚµÄIO¿ªÏúºÍÆÚ´ýʱ¼ä¡£Ê¹ÓÃг̣¬¿ÉÒÔ½«¶àÌõSQLÓï¾äºÏ²¢³ÉÒ»´ÎÅúÁ¿Ö´ÐУ¬ïÔÌIO¿ªÏúºÍÆÚ´ýʱ¼ä£¬Ìá¸ßÊý¾Ý¿â»áÊÕЧÂÊ¡£Ê¾Àý´úÂëÈçÏ£º
//½¨ÉèÅþÁ¬³Ø $pool = new SwooleCoroutineChannel(50); //г̳ØʹÃü function db_task($sql) { $conn = $pool->pop(); $result = $conn->query($sql); $pool->push($conn); return $result; } //½¨ÉèÐ³Ì go(function () { $sqls = [ "SELECT * FROM users WHERE id = 1", "SELECT * FROM users WHERE id = 2", "SELECT * FROM users WHERE id = 3", //... ]; $sql = implode(";", $sqls); //ºÏ²¢SQLÓï¾ä $result = db_task($sql); foreach ($result as $row) { var_dump($row); } });
µÇ¼ºó¸´ÖÆ
ËÄ¡¢×ܽá
ͨ¹ýʹÓÃSwooleµÄгÌÌØÕ÷£¬ÎÒÃÇ¿ÉÒÔÓÅ»¯MySQLÊý¾Ý¿â»á¼û£¬Ìá¸ßWebÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£ÏêϸÀ´Ëµ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÅþÁ¬³Ø¡¢Ð³ÌMySQL¿Í»§¶ËºÍÅúÁ¿´¦Öóͷ£SQLÓï¾äµÈÊÖ¶ÎÀ´ÓÅ»¯Êý¾Ý¿â»á¼û¡£ËäÈ»£¬Ð³Ì²»µ«½ö¾ÖÏÞÓÚMySQLÊý¾Ý¿â»á¼û£¬ËüÔÚÍøÂçͨѶ¡¢×¼Ê±Ê¹Ãü¡¢Îļþ¶ÁдµÈ³¡¾°ÖÐÒ²ÓкܺõÄÓ¦Óá£ÈÃÎÒÃÇÒ»Æð×îÏÈSwooleµÄʵ¼ùÖ®Âðɣ¡
ÒÔÉϾÍÊÇSwooleʵ¼ù£ºÔõÑùʹÓÃгÌÓÅ»¯Êý¾Ý¿â»á¼ûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡