Swoole½ø½×£ºÔõÑùʹÓöàÀú³ÌÌáÉýPHP´¦ÀíÄÜÁ¦
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬¹ØÓÚwebÓ¦ÓÃÐÔÄܺͲ¢·¢´¦ÀíÄÜÁ¦µÄÒªÇóÒ²Ô½À´Ô½¸ß¡£¶ø¹ØÓÚphpÓïÑÔµÄÓ¦ÓóÌÐò¶øÑÔ£¬ÓÉÓÚÆäµ¥Ï̵߳ÄÌØÕ÷£¬Ôڸ߲¢·¢³¡¾°ÏºÜÈÝÒ×·ºÆðÐÔÄÜÆ¿¾±¡£¶øswoole×÷Ϊһ¿î¸ßÐÔÄܵÄÒì²½ÍøÂçͨѶ¿ò¼Ü£¬Äܹ»ÓÐÓõØÌáÉýphpÓ¦ÓóÌÐòµÄ´¦ÀíÄÜÁ¦£¬´Ó¶øʹÆäÄܹ»Ôڸ߲¢·¢³¡¾°ÏÂÌåÏÖ¾«²Ê¡£
ÔÚSwooleµÄ¹Ù·½ÎĵµÖУ¬ÎÒÃÇ¿ÉÒÔ·¢Ã÷ËüÖ§³Ö¶àÀú³ÌµÄ±à³Ì·½·¨¡£ÕâÖÖ·½·¨¿ÉÒÔÈÃÎÒÃǽ«Ò»¸ö´óʹÃüƽ¾ù·ÖÅɸø¶à¸öÀú³ÌÈ¥Ö´ÐУ¬´Ó¶øÔÚÒ»¶¨Ë®Æ½ÉÏÌáÉýÁËPHPÓ¦ÓóÌÐòµÄ´¦ÀíÄÜÁ¦¡£Í¬Ê±£¬»ùÓÚ¶àÀú³ÌµÄ±à³Ì·½·¨Ò²Äܹ»ÊµÏÖÔ½·¢ÎÞаµÄÀú³ÌÖÎÀíºÍ×ÊÔ´µ÷Àí£¬ÓÐÓõØÌá¸ß³ÌÐòµÄÎȹÌÐԺͿɿ¿ÐÔ¡£ÏÂÃ棬ÎÒÃǽ«ÏêϸÏÈÈÝÔõÑùʹÓÃSwooleµÄ¶àÀú³Ì±à³Ì·½·¨ÌáÉýPHP´¦ÀíÄÜÁ¦¡£
ʲôÊǶàÀú³Ì£¿
¼òÆÓÀ´Ëµ£¬Àú³Ì¾ÍÊÇÕýÔÚÔËÐеijÌÐò¡£¶àÀú³Ì¼´Ö¸Ò»¸ö³ÌÐò·Ö³É¶à¸öÀú³ÌÈ¥Ö´ÐС£
ÔÚUnix/LinuxϵͳÖУ¬Ã¿¸öÀú³Ì¶¼ÓÐÒ»¸öΨһµÄÀú³ÌID£¬Í¨¹ý¸ÃID¿ÉÒÔÇø·Ö²î±ðµÄÀú³Ì¡£¶àÀú³Ì±à³ÌµÄʵÖÊÊǽ«Ò»¸ö³ÌÐò·Ö³É¶à¸öÀú³ÌÈ¥Ö´ÐУ¬Ã¿¸öÀú³ÌÓµÓÐ×ÔÁ¦µÄµØµã¿Õ¼ä¡¢Êý¾ÝÕ»¡¢Ö¸Áî¼ÆÊýÆ÷µÈ×ÊÔ´¡£Àú³ÌÖ®¼äµÄͨѶ¿ÉÒÔͨ¹ýÐÂÎÅÐÐÁС¢¹ÜµÀ¡¢¹²ÏíÄÚ´æµÈ·½·¨ÊµÏÖ¡£
ΪʲôҪʹÓöàÀú³Ì£¿
ʹÓöàÀú³Ì±à³Ì¿ÉÒÔ´øÀ´ÒÔϼ¸¸öÀûÒ棺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÌáÉý´¦ÀíÄÜÁ¦£º¶àÀú³Ì±à³Ì¿ÉÒÔ½«Ò»¸ö´óʹÃüƽ¾ù·ÖÅɸø¶à¸öÀú³ÌÈ¥Ö´ÐУ¬´Ó¶øÌáÉý³ÌÐòµÄ´¦ÀíÄÜÁ¦ºÍ²¢·¢ÐÔÄÜ¡£
ÎȹÌÐԺͿɿ¿ÐÔ£º¶àÀú³Ì±à³Ì¿ÉÒÔͨ¹ýÀú³ÌÖÎÀíºÍ×ÊÔ´µ÷Àí£¬Ìá¸ß³ÌÐòµÄÎȹÌÐԺͿɿ¿ÐÔ¡£
²¢ÐÐÅÌË㣺¶àÀú³Ì±à³Ì¿ÉÒÔÈòî±ðµÄÀú³ÌͬʱִÐÐÅÌËãʹÃü£¬´Ó¶øʵÏÖ²¢ÐÐÅÌËã¡£
ÔõÑùÔÚSwooleÖÐʹÓöàÀú³Ì£¿
ÏÂÃæÏÈÈÝ»ùÓÚSwooleµÄ¶àÀú³Ì±à³Ì·½·¨£¬À´×ÊÖúÎÒÃÇÌáÉýPHPÓ¦ÓóÌÐòµÄ´¦ÀíÄÜÁ¦¡£
½¨Éè×ÓÀú³Ì
ÔÚSwooleÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃswoole_processÀàÀ´½¨Éè×ÓÀú³Ì¡£swoole_processÖÐÌṩÁËÐí¶àÒªÁ죬°üÀ¨½¨Éè×ÓÀú³Ì¡¢Ïò×ÓÀú³Ì·¢ËÍÐÂÎÅ¡¢¶ÁÈ¡×ÓÀú³ÌµÄstdoutºÍstderrµÈµÈ¡£
ÒÔÏ´úÂëÑÝʾÁËÔõÑùʹÓÃswoole_process½¨ÉèÒ»¸ö×ÓÀú³Ì£º
$process = new swoole_process(function(swoole_process $process) { // ÕâÀïÊÇ×ÓÀú³ÌµÄÂß¼´¦Àí´úÂë $process->write("Hello from child process "); }); $process->start(); swoole_event_wait();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ýnew swoole_process()½¨ÉèÁËÒ»¸öеÄ×ÓÀú³Ì£¬´«ÈëµÄ²ÎÊýÊÇÒ»¸öÄäÃûº¯Êý£¬¸Ãº¯ÊýÖаüÀ¨ÁË×ÓÀú³ÌµÄ´¦ÀíÂß¼¡£$process->start()ÒªÁì¿ÉÒÔÆô¶¯×ÓÀú³Ì¡£swoole_event_wait()ÒªÁìÓÃÓÚÆÚ´ý×ÓÀú³ÌÖ´ÐÐÍê±Ï£¬ÎÒÃÇ¿ÉÒÔÃ÷ȷΪËü»áÛÕ±ÕÄ¿½ñÀú³Ì£¬Ö±µ½×ÓÀú³Ì¿¢Ê¡£
¸¸×ÓÀú³ÌͨѶ
ÔÚ±ê×¼µÄUnix/LinuxϵͳÖУ¬Àú³ÌÖ®¼äµÄͨѶ¿ÉÒÔͨ¹ý¹ÜµÀ£¨pipe£©¡¢ÐÂÎÅÐÐÁУ¨message queue£©¡¢¹²ÏíÄڴ棨shared memory£©¡¢Ðźţ¨signal£©µÈ·½·¨ÊµÏÖ¡£ÔÚSwooleÖУ¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃÕâЩ·½·¨ÊµÏÖ¸¸×ÓÀú³ÌÖ®¼äµÄͨѶ¡£
ÒÔÏ´úÂëÑÝʾÁËÔõÑùÔÚ¸¸×ÓÀú³ÌÖ®¼äͨ¹ý¹ÜµÀͨѶ£º
$process = new swoole_process(function(swoole_process $process) { // ×ÓÀú³Ì£º´Ó¸¸Àú³Ì¹ÜµÀÖжÁÈ¡ÐÂÎÅ $msg = $process->read(); echo "Message from parent process: $msg "; $process->exit(); }); $process->start(); // ¸¸Àú³Ì£ºÏò×ÓÀú³Ì¹ÜµÀÖÐдÈëÐÂÎÅ $process->write("Hello from parent process"); // ÆÚ´ý×ÓÀú³Ì¿¢Ê swoole_process::wait();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÔÚ×ÓÀú³ÌÖÐŲÓÃ$process->read()ÒªÁì´Ó¸¸Àú³Ì¹ÜµÀÖжÁÈ¡ÐÂÎÅ£¬È»ºóͨ¹ýecho´òÓ¡³öÀ´¡£ÔÚ¸¸Àú³ÌÖУ¬ÎÒÃÇͨ¹ý$process->write()ÒªÁìÏò×ÓÀú³Ì¹ÜµÀÖÐдÈëÐÂÎÅ¡£
Àú³Ì³Ø
ÈôÊÇÎÒÃÇÐèÒªÆô¶¯¶à¸ö×ÓÀú³ÌÀ´´¦ÀíʹÃü£¬ÄÇôÿ¸ö×ÓÀú³Ì¶¼ÐèÒª»®·Ö½¨ÉèºÍÖÎÀíÊÇÒ»¼þ½ÏÁ¿Æ¶À§µÄÊÂÇé¡£Õë¶ÔÕâÖÖÇéÐΣ¬SwooleÌṩÁËswoole_process::pool()ÒªÁìÀ´ÊµÏÖÀú³Ì³ØµÄ¿´·¨¡£Àú³Ì³Ø¿ÉÒÔʵÏÖ¶à¸ö×ÓÀú³ÌµÄ¸´Ó㬴ӶøÓÅ»¯Àú³ÌÖÎÀíºÍ×ÊÔ´µ÷Àí¡£
ÒÔÏ´úÂëÑÝʾÁËÔõÑùʹÓÃÀú³Ì³Ø½¨Éè¶à¸ö×ÓÀú³ÌÀ´´¦ÀíʹÃü£º
$pool = new swoole_process_pool(2, SWOOLE_IPC_UNIXSOCK, 0, true); // ÉèÖÃÀú³Ì³ØÆô¶¯»Øµ÷º¯Êý $pool->on("workerStart", function(swoole_process_pool $pool, $worker_id) { echo "Worker $worker_id started "; }); // ÉèÖÃÀú³Ì³ØʹÃü´¦Àíº¯Êý $pool->on("message", function(swoole_process_pool $pool, $task) { echo "Task received: $task "; }); // Æô¶¯Àú³Ì³Ø $pool->start(); // ÍùÀú³Ì³ØÖÐͶµÝʹÃü $pool->write("Hello"); // ÆÚ´ýʹÃüÖ´ÐÐÍê±Ï $pool->shutdown();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ýswoole_process_poolÀཨÉèÁËÒ»¸öÀú³Ì³Ø£¬ÆäÖвÎÊý2ÌåÏÖʹÓÃUNIXÓòÌ×½Ó×ÖͨѶ£¬²ÎÊý0ÌåÏÖ²»×öÏÞÖƵØʹÓÃËùÓÐCPUÄںˣ¬²ÎÊýtrueÌåÏÖ×Ô¶¯ÖØÆôéæÃüµÄ×ÓÀú³Ì¡£ÔÚÉèÖÃÀú³Ì³ØÆô¶¯»Øµ÷º¯ÊýºÍʹÃü´¦Àíº¯Êýºó£¬ÎÒÃÇͨ¹ý$pool->start()ÒªÁìÆô¶¯Àú³Ì³Ø¡£ÔÚͶµÝÍêʹÃüºó£¬ÎÒÃÇͨ¹ý$pool->shutdown()ÒªÁìÆÚ´ýʹÃüÖ´ÐÐÍê±Ï¡£
×ܽá
ͨ¹ýʹÓÃSwooleÖеĶàÀú³Ì±à³Ì·½·¨£¬ÎÒÃÇ¿ÉÒÔÓÐÓõØÌáÉýPHPÓ¦ÓóÌÐòµÄ´¦ÀíÄÜÁ¦ºÍ²¢·¢ÐÔÄÜ¡£Ê¹ÓöàÀú³ÌµÄÓÅ»¯·½·¨Äܹ»ÊµÏÖÔ½·¢ÎÞаµÄÀú³ÌÖÎÀíºÍ×ÊÔ´µ÷Àí£¬Ìá¸ß³ÌÐòµÄÎȹÌÐԺͿɿ¿ÐÔ¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇÐèҪ͎á×Ô¼ºµÄÓªÒµÐèÇóºÍϵͳ×ÊÔ´À´Ñ¡ÔñºÏÊʵÄÀú³ÌÊýºÍÓÅ»¯·½·¨£¬ÒÔµÖ´ï¸üºÃµÄÐÔÄܺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇSwoole½ø½×£ºÔõÑùʹÓöàÀú³ÌÌáÉýPHP´¦ÀíÄÜÁ¦µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡