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.