Pthreads-for help. How does php use Pthread for multi-process?

Source: Internet
Author: User
Tags usleep
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?

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.