PHP multi-process practice (3)

Source: Internet
Author: User
PHP multi-process practice (III) 5. windows multi-thread windows does not support the pcntl function. Fortunately, the tool curl_multi_exec () uses internal multi-thread to access multiple links. each link can be used as a task. Write the script test1.php & lt ;? Phpdate_default_timezone PHP multi-process practice (III)
5. windows multithreadingWindows does not support the pcntl function. Fortunately, the tool curl_multi_exec () uses internal multithreading to access multiple links. each link can be used as a task.
Write the script test1.php
  $task){     $ch[$i] = curl_init();     curl_setopt($ch[$i], CURLOPT_URL, $task);     curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);     curl_multi_add_handle($mh, $ch[$i]);}do {$mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);while ($active && $mrc == CURLM_OK) {     if (curl_multi_select($mh) != -1) {       do {$mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);     }}// completed, checkout resultforeach ($tasks as $j => $task){     if (curl_error($ch[$j])){          echo "task ${j} [$task ] error " , curl_error($ch[$j]), "\r\n" ;     } else {          echo "task ${j} [$task ] get: \r\n" , curl_multi_getcontent($ch[$j]), "\r\n" ;     }}?>



Write the script test2.php
 



Run the command line# Php-f test1.php &
Output resultTask 0 [http: // localhost/feedbowl/t2.php? Job = task1] get:
Child start, pid 5804
5804 20:22:35
5804 20:22:36
5804 20:22:37
5804 20:22:38
5804 20:22:39

Task 1 [http: // localhost/feedbowl/t2.php? Job = task2] get:
Child start, pid 5804
5804 20:22:35
5804 20:22:36
5804 20:22:37
5804 20:22:38
5804 20:22:39

Task 2 [http: // localhost/feedbowl/t2.php? Job = task3] get:
Child start, pid 5804
5804 20:22:35
5804 20:22:36
5804 20:22:37
5804 20:22:38
5804 20:22:39

As shown in the print TIME, multiple tasks run almost simultaneously.
Related Article

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.