There is no problem with running the following code. How many processes are produced when the $ urls array has data? If there are tens of thousands of data, will there be tens of thousands of processes? What should I do if I want to set only 10 processes to process the $ urls array? {Code...} There is no problem with the following code. I feel that the $ urls array contains tens of thousands of processes. If there are tens of thousands of data, will there be tens of thousands of processes?
What should I do if I want to set only 10 processes to process the $ urls array?
Arg = $ arg;} public function run () {if ($ this-> arg) {// echo $ this-> result = $ this-> arg; $ this-> result = model_http_curl_get ($ this-> arg) ;}}$ thread = new DuoXianCheng ("World"); if ($ thread-> start ()) {$ thread-> join ();} function model_http_curl_get ($ url) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_TIMEOUT, 5); curl_setopt ($ curl, CURLOPT_USERAGENT, 'mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2 )'); $ result = curl_exec ($ curl); curl_close ($ curl); return $ result;} for ($ I = 0; $ I <10; $ I ++) {$ urls [] =' http://www.baidu.com/s?wd= '. Rand (10000,200 00);}/* multithread speed test */$ t = microtime (true); foreach ($ urls as $ key => $ url) {$ workers [$ key] = new \ DuoXianCheng ($ url); $ workers [$ key]-> start ();} foreach ($ workers as $ key =>$ worker) {while ($ workers [$ key]-> isRunning () {usleep (100 );} if ($ workers [$ key]-> join () {var_dump ($ workers [$ key]-> result) ;}}$ e = microtime (true ); echo "multithreading time consumption :". ($ e-$ t ). "seconds
";/* Single-thread speed test */$ t = microtime (true); foreach ($ urls as $ key => $ url) {var_dump (model_http_curl_get ($ url) ;}$ e = microtime (true); echo "For loop time consumption :". ($ e-$ t ). "seconds
";?>
Reply content:
There is no problem with running the following code. How many processes are produced when the $ urls array has data? If there are tens of thousands of data, will there be tens of thousands of processes?
What should I do if I want to set only 10 processes to process the $ urls array?
Arg = $ arg;} public function run () {if ($ this-> arg) {// echo $ this-> result = $ this-> arg; $ this-> result = model_http_curl_get ($ this-> arg) ;}}$ thread = new DuoXianCheng ("World"); if ($ thread-> start ()) {$ thread-> join ();} function model_http_curl_get ($ url) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_TIMEOUT, 5); curl_setopt ($ curl, CURLOPT_USERAGENT, 'mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2 )'); $ result = curl_exec ($ curl); curl_close ($ curl); return $ result;} for ($ I = 0; $ I <10; $ I ++) {$ urls [] =' http://www.baidu.com/s?wd= '. Rand (10000,200 00);}/* multithread speed test */$ t = microtime (true); foreach ($ urls as $ key => $ url) {$ workers [$ key] = new \ DuoXianCheng ($ url); $ workers [$ key]-> start ();} foreach ($ workers as $ key =>$ worker) {while ($ workers [$ key]-> isRunning () {usleep (100 );} if ($ workers [$ key]-> join () {var_dump ($ workers [$ key]-> result) ;}}$ e = microtime (true ); echo "multithreading time consumption :". ($ e-$ t ). "seconds
";/* Single-thread speed test */$ t = microtime (true); foreach ($ urls as $ key => $ url) {var_dump (model_http_curl_get ($ url) ;}$ e = microtime (true); echo "For loop time consumption :". ($ e-$ t ). "seconds
";?>
Curl_init () does not open a new process, but the file descriptor of the client is limited, and the memory is limited, meaning that the new Thread is not infinite. To achieve the goal of pthread, check http://php.net/manual/en/function.curl-multi init.php to allow you to process multiple curl_init () returns in different steps.
The Thread class is a Thread, not a process.
Create ten DuoXianCheng objects:
For ($ I = 0; $ I <= 9; $ I ++ ){
$ Obj [$ I] = new DuoXianCheng ("World ");
$ Obj [$ I]-> start ();
}
Thank you for your reply! I mean a thread, not a process. I typed it wrong!
What should I do if $ urls has 100,000 data records and only has 10 threads?