In Swoole, PHP can implement multiple processes in PHP by the way it starts child processes:

<? php$s_time = time();

echo 'Start time :'.date('H:i:s'.$s_time).PHP_EOL; / / process$work_number= 6; //$worker= []; // Simulate the address$curl= ['https://blog.csdn.net/feiwutudou'.'https://wiki.swoole.com/wiki/page/215.html'.'http://fanyi.baidu.com/?aldtype=16047#en/zh/manager'.'http://wanguo.net/Salecar/index.html'.'http://o.ngking.com/themes/mskin/login/login.jsp'.'https://blog.csdn.net/marksinoberg/article/details/77816991']; // single thread mode // foreach ($curl as $v{/ /echo curldeta($v); // create a processfor ($i= 0;$i < $work_number; $i++) {// Create multiple threads$pro=new swoole_process(function(swoole_process $work) use($i.$curl){// Get the HTML file$content=curldeta($curl[$i]); // Write pipe$work->write($content.PHP_EOL);

    },true);

    $pro_id=$pro->start();

    $worker[$pro_id] =$pro; } // read the contents of the pipe$worker as $v) {

    echo $v->read().PHP_EOL; } // Simulate crawlerfunction curldeta($curl_arr)

{//file_get_contents

    echo $curl_arr.PHP_EOL;

    file_get_contents($curl_arr); } // Process returns swoole_process::wait(a);$e_time = time();

echo 'End time :'.date('H:i:s'.$e_time).PHP_EOL;

  

echo 'Time spent :'. ($e_time-$s_time).'秒'.PHP_EOL; ? >Copy the code

Multithreaded execution results:

As a comparison, single thread results:

The improvement is obvious!

So that’s how PHP uses Swoole to implement multi-threaded crawlers

To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.