PHP multi-thread internal multi-thread instance analysis-PHP source code

Source: Internet
Author: User
Tags php multithreading
This article mainly introduces the internal multithreading of PHP multithreading. The example analyzes the usage skills of php multithreading and has some reference value, for more information, see the next article. This article mainly introduces the internal multithreading of PHP multithreading. The example analyzes the usage skills of php multithreading and has some reference value. For more information, see

This article analyzes the internal multithreading usage of PHP multithreading. Share it with you for your reference. The details are as follows:

 SetOptions ($ options) ;}// set the url list function setUrls ($ urls) {$ this-> urls = $ urls; return $ this ;} // set the option function setOptions ($ options) {$ options [CURLOPT_RETURNTRANSFER] = 1; if (isset ($ options ['http _ post']) {curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ options ['http _ post']); unset ($ options ['http _ post']);} if (! Isset ($ options [CURLOPT_USERAGENT]) {$ options [CURLOPT_USERAGENT] = 'mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;) ';} if (! Isset ($ options [CURLOPT_FOLLOWLOCATION]) {$ options [CURLOPT_FOLLOWLOCATION] = 1;} if (! Isset ($ options [CURLOPT_HEADER]) {$ options [CURLOPT_HEADER] = 0 ;}$ this-> options = $ options ;}// capture all content in parallel function exec () {if (empty ($ this-> urls) |! Is_array ($ this-> urls) {return false;} $ curl = $ data = array (); $ mh = curl_multi_init (); foreach ($ this-> urls as $ k => $ v) {$ curl [$ k] = $ this-> addHandle ($ mh, $ v );} $ this-> execMulitHandle ($ mh); foreach ($ this-> urls as $ k => $ v) {$ data [$ k] = curl_multi_getcontent ($ curl [$ k]); curl_multi_remove_handle ($ mh, $ curl [$ k]);} curl_multi_close ($ mh ); return $ data;} // capture the content of only one webpage. Function execOne ($ url) {if (empty ($ url) {return false;} $ ch = curl_init ($ url); $ this-> setOneOption ($ ch ); $ content = curl_exec ($ ch); curl_close ($ ch); return $ content;} // Internal function, set the private function setOneOption ($ ch) option of a handle) {curl_setopt_array ($ ch, $ this-> options);} // add a new parallel handle private function addHandle ($ mh, $ url) {$ ch = curl_init ($ url); $ this-> setOneOption ($ ch); curl_multi_add_han Dle ($ mh, $ ch); return $ ch;} // parallel execution (this is a common mistake. I still use this method here, this write method // downloading a small file may cause 100% of CPU usage, and this loop will run more than 0.1 million times. // This is a typical error caused by ignorance of the principle. This error is quite common in official PHP documents .) Private function execMulitHandle ($ mh) {$ running = null; do {curl_multi_exec ($ mh, $ running) ;}while ($ running> 0 );}} /* The following is an example of the test of the above class: */$ urls = array (" http://baidu.com "," http://baidu.com "," http://baidu.com "," http://baidu.com "," http://baidu.com "," http://baidu.com "," http://www.google.com "," http://www.sina.com.cn ",); $ M = new Http_MultiRequest (); $ t = microtime (true); $ m-> setUrls ($ urls); // parallel fetch (parallel capture ): $ data = $ m-> exec (); $ parallel_time = microtime (true)-$ t; echo $ parallel_time. "\ n"; $ t = microtime (true); // serial fetch (serial capture): foreach ($ urls as $ url) {$ data [] = $ m-> execOne ($ url);} $ serial_time = microtime (true)-$ t; echo $ serial_time. "\ n ";

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.