×ðÁú¿­Ê±

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ʵ¼ù£ºÔõÑùʹÓÃЭ³ÌÓÅ»¯Êý¾Ý¿â»á¼ûµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

13452372176

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿