PHP Multi-process practice

Source: Internet
Author: User
PHP Multi-process practice

1. Direct Way

Pcntl_fork () creates a process in which the parent process returns a value that is the PID of the child process, and the return value in the child process is 0,-1 indicates that the creation process failed. Very similar to C.

Test Script test.php

//example of multiple processes Date_default_timezone_se    T (' asia/chongqing ');    echo "Parent start, pid", Getmypid (), "\ n";    Beep ();           for ($i =0; $i <3; + + $i) {$pid = Pcntl_fork ();          if ($pid = =-1) {die ("Cannot fork");                 } else if ($pid > 0) {echo "parent continue \ n";                for ($k =0; $k <2; + + $k) {beep ();                 }} else if ($pid = = 0) {echo "Child start, pid", Getmypid (), "\ n";                for ($j =0; $j <5; + + $j) {beep ();          } exit;          }}//* * * function beep () {echo getmypid (), "\ T", Date (' y-m-d h:i:s ', Time ()), "\ n";    Sleep (1); }

Run with command line
#php-F test.php

Output results
Parent start, PID 1793
1793 2013-01-14 15:04:17
Parent continue
1793 2013-01-14 15:04:18
Child start, PID 1794
1794 2013-01-14 15:04:18
1794 2013-01-14 15:04:19
1793 2013-01-14 15:04:19
1794 2013-01-14 15:04:20
Parent continue
1793 2013-01-14 15:04:20
Child start, PID 1795
1795 2013-01-14 15:04:20
17931794 2013-01-14 15:04:212013-01-14 15:04:21

1795 2013-01-14 15:04:21
1794 2013-01-14 15:04:22
1795 2013-01-14 15:04:22
Parent continue
1793 2013-01-14 15:04:22
Child start, PID 1796
1796 2013-01-14 15:04:22
1793 2013-01-14 15:04:23
1796 2013-01-14 15:04:23
1795 2013-01-14 15:04:23
1795 2013-01-14 15:04:24
1796 2013-01-14 15:04:24
1796 2013-01-14 15:04:25
1796 2013-01-14 15:04:26

As you can see, 3 child processes are created and run in parallel with the parent process. One of the lines is a little different from the others,
17931794 2013-01-14 15:04:212013-01-14 15:04:21
The conflict is caused by the simultaneous write operation of two processes.

2. Blocking mode

In a direct way, after the parent process creates the child process, it does not wait for the child process to end, but continues to run. There seems to be no problem here. If the PHP script does not end automatically after it finishes running, it will cause the child process to be unable to reclaim because it is resident memory. That's the zombie process. You can wait for the process to end through the Pcntl_wai () method, and then reclaim the process that has ended.
Change the test script to:

$pid = Pcntl_fork (), if ($pid = =-1) {    ...} else if ($pid > 0) {     echo "parent continue \ n";     Pcntl_wait ($status);     for ($k =0; $k <2; + + $k) {          beep ();    }} else if ($pid = = 0) {     ...}

Run with command line
#php-F test.php

Output results
Parent start, PID 1807
1807 2013-01-14 15:20:05
Parent continue
Child start, PID 1808
1808 2013-01-14 15:20:06
1808 2013-01-14 15:20:07
1808 2013-01-14 15:20:08
1808 2013-01-14 15:20:09
1808 2013-01-14 15:20:10
1807 2013-01-14 15:20:11
1807 2013-01-14 15:20:12
Parent continue
Child start, PID 1809
1809 2013-01-14 15:20:13
1809 2013-01-14 15:20:14
1809 2013-01-14 15:20:15
1809 2013-01-14 15:20:16
1809 2013-01-14 15:20:17
1807 2013-01-14 15:20:18
1807 2013-01-14 15:20:19
Child start, PID 1810
1810 2013-01-14 15:20:20
Parent continue
1810 2013-01-14 15:20:21
1810 2013-01-14 15:20:22
1810 2013-01-14 15:20:23
1810 2013-01-14 15:20:24
1807 2013-01-14 15:20:25
1807 2013-01-14 15:20:26

The parent process blocks itself in pcntl_wait (), waiting for the child process to run.

3. Non-blocking mode

Blocking mode loses the parallelism of many processes. There is also a way to reclaim a child process that has ended and can be parallel. This is a non-blocking approach.
To modify a script:

    Example of multiple processes Date_default_timezone_set (' asia/chongqing ');    DECLARE (ticks = 1);    Pcntl_signal (SIGCHLD, "garbage");    echo "Parent start, pid", Getmypid (), "\ n";    Beep ();           for ($i =0; $i <3; + + $i) {$pid = Pcntl_fork ();          if ($pid = =-1) {die ("Cannot fork");                 } else if ($pid > 0) {echo "parent continue \ n";                for ($k =0; $k <2; + + $k) {beep ();                 }} else if ($pid = = 0) {echo "Child start, pid", Getmypid (), "\ n";                for ($j =0; $j <5; + + $j) {beep ();          } exit (0);    }}//Parent while (1) {//do something else sleep (5);                     }//* * * function garbage ($signal) {echo "SiGNeL $signal received\n"; while ($pid = Pcntl_waitpid ( -1, $status, Wnohang) > 0) {echo "\ t Child End PID $pid, status $status \ n ";          }} function beep () {echo getmypid (), "\ T", Date (' y-m-d h:i:s ', Time ()), "\ n";    Sleep (1); }

Run with command line
#php-F test.php &

Output results
Parent start, PID 2066
2066 2013-01-14 16:45:34
Parent continue
2066 2013-01-14 16:45:35
Child start, PID 2067
2067 2013-01-14 16:45:35
20662067 2013-01-14 16:45:362013-01-14 16:45:36

2067 2013-01-14 16:45:37
Parent continue
2066 2013-01-14 16:45:37
Child start, PID 2068
2068 2013-01-14 16:45:37
2067 2013-01-14 16:45:38
2068 2013-01-14 16:45:38
2066 2013-01-14 16:45:38
Parent continue
2066 2013-01-14 16:45:40
Child start, PID 2069
2069 2067 2013-01-14 16:45:40
2013-01-14 16:45:40
2068 2013-01-14 16:45:40
2066 2013-01-14 16:45:41
2069 2013-01-14 16:45:41
2068 2013-01-14 16:45:41
SiGNeL received
Child end PID 2067, status 0
2069 2013-01-14 16:45:42
2068 2013-01-14 16:45:42
2069 2013-01-14 16:45:43
SiGNeL received
Child end PID 2068, status 0
2069 2013-01-14 16:45:44
SiGNeL received
Child end PID 2069, status 0

Multiple processes run in parallel and run for about 10 seconds, using Ps-ef | grep php to view a running process with only one process
Lqling 2066 1388 0 16:45 pts/1 00:00:00 php-f t5.php
Is the parent process, and the child process is recycled.

4. Child Process Exit status


Pcntl_waitpid ( -1, $status, Wnohang) $status returns the end state of the child process

5. Windows multithreading

Windows does not support the PCNTL function, fortunately there is curl_multi_exec () This tool, the use of internal multi-threading, access to multiple links, each link can be used as a task.

Writing a script test1.php

    Date_default_timezone_set (' asia/chongqing '); $tasks = Array (' Http://localhost/feedbowl/t2.php?job=task1 ', ' Http://localhost/feedbowl/t2.php?job=task2 '    , ' Http://localhost/feedbowl/t2.php?job=task3 ');    $MH = Curl_multi_init ();         foreach ($tasks as $i = = $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), and 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 result foreach ($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"; }    }

Writing a script test2.php

    Date_default_timezone_set (' asia/chongqing ');    echo "Child start, pid", Getmypid (), "\ r \ n";    for ($i =0; $i <5; + + $i) {         beep ();    }    Exit (0);    function beep () {        echo getmypid (), "\ T", Date (' y-m-d h:i:s ', Time ()), "\ r \ n";        Sleep (1);    }

Run with command line
#php-F test1.php &

Output results
Task 0 [Http://localhost/feedbowl/t2.php?job=task1] Get:
Child start, PID 5804
5804 2013-01-15 20:22:35
5804 2013-01-15 20:22:36
5804 2013-01-15 20:22:37
5804 2013-01-15 20:22:38
5804 2013-01-15 20:22:39

Task 1 [http://localhost/feedbowl/t2.php?job=task2] Get:
Child start, PID 5804
5804 2013-01-15 20:22:35
5804 2013-01-15 20:22:36
5804 2013-01-15 20:22:37
5804 2013-01-15 20:22:38
5804 2013-01-15 20:22:39

Task 2 [HTTP://LOCALHOST/FEEDBOWL/T2.PHP?JOB=TASK3] Get:
Child start, PID 5804
5804 2013-01-15 20:22:35
5804 2013-01-15 20:22:36
5804 2013-01-15 20:22:37
5804 2013-01-15 20:22:38
5804 2013-01-15 20:22:39

As seen from the time of printing, multiple tasks run almost simultaneously.

  • 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.