search

​PHP多进程实践

1. 直接方式

pcntl_fork() 创建一个进程,在父进程返回值是子进程的pid,在子进程返回值是0,-1表示创建进程失败。跟C非常相似。

测试脚本 test.php

    // example of multiple processes
    date_default_timezone_set( '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( &#39;Y-m-d H:i:s&#39;, time()), "\n" ;
          sleep(1);
    }

用命令行运行
#php -f test.php

输出结果
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

从中看到,创建了3个子进程,和父进程一起并行运行。其中有一行格式跟其他有些不同,
17931794                2013-01-14 15:04:212013-01-14 15:04:21
因为两个进程同时进行写操作,造成了冲突。

2. 阻塞方式

用直接方式,父进程创建了子进程后,并没有等待子进程结束,而是继续运行。似乎这里看不到有什么问题。如果php脚本并不是运行完后自动结束,而是常驻内存的,就会造成子进程无法回收的问题。也就是僵尸进程。可以通过pcntl_wai()方法等待进程结束,然后回收已经结束的进程。
将测试脚本改成:

$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){
     ...
}

用命令行运行
#php -f test.php

输出结果
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

父进程在pcntl_wait()将自己阻塞,等待子进程运行完了才接着运行。

3. 非阻塞方式

阻塞方式失去了多进程的并行性。还有一种方法,既可以回收已经结束的子进程,又可以并行。这就是非阻塞的方式。
修改脚本:

    // example of multiple processes
    date_default_timezone_set( &#39;Asia/Chongqing&#39;);
    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( &#39;Y-m-d H:i:s&#39;, time()), "\n" ;
          sleep(1);
    }

用命令行运行
#php -f test.php &

输出结果
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 17 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 17 received
         child end pid 2068, status 0
2069    2013-01-14 16:45:44
signel 17 received
         child end pid 2069, status 0

多个进程又并行运行了,而且运行大约10秒钟之后,用 ps -ef | grep php 查看正在运行的进程,只有一个进程
lqling    2066  1388  0 16:45 pts/1    00:00:00 php -f t5.php
是父进程,子进程被回收了。

4. 子进程退出状态


pcntl_waitpid(-1, $status, WNOHANG) $status 返回子进程的结束状态

5. windows多线程

windows系统不支持pcntl函数,幸好有curl_multi_exec()这个工具,利用内部的多线程,访问多个链接,每个链接可以作为一个任务。

编写脚本 test1.php

    date_default_timezone_set( &#39;Asia/Chongqing&#39;);
    $tasks = array(
         &#39;http://localhost/feedbowl/t2.php?job=task1&#39;,
         &#39;http://localhost/feedbowl/t2.php?job=task2&#39;,
         &#39;http://localhost/feedbowl/t2.php?job=task3&#39;
    );
    $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); } 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" ;
         }
    }

编写脚本 test2.php

    date_default_timezone_set( &#39;Asia/Chongqing&#39;);
    echo "child start, pid ", getmypid(), "\r\n" ;
    for ($i=0; $i<5; ++$i){
         beep();
    }
    exit (0);
    // ***
    function beep(){
        echo getmypid(), "\t" , date(&#39;Y-m-d H:i:s&#39; , time()), "\r\n";
        sleep(1);
    }

用命令行运行
#php -f test1.php &

输出结果
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

从打印的时间看到,多个任务几乎是同时运行的。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment