Introduction to the usage of PHP's Pcntl multi-process extension

不言
Release: 2023-04-05 11:52:02
forward
2829 people have browsed it

This article brings you an introduction to the usage of PHP's Pcntl multi-process extension. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP provides a Pcntl extension. Pcntl is based on the Linux process. Windows systems do not have this extension for the time being. It is used to implement basic multi-process implementation. You can use this to handle a time-consuming task, such as sending subscription emails. , text messages, site messages, etc.

Sample code:

The code is based on the Laravel framework. It is recommended to use this framework to allow this part of the code.

public function index(Request $request)

    {

        pcntl_signal(SIGCHLD, SIG_IGN); //如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。  

        $max = 8000;

        $workers = 10;

        

        $pids = array();

        for($i = 0; $i < $workers; $i++){

            $pids[$i] = pcntl_fork();

            $pid = posix_getpid();

            switch ($pids[$i]) {

                case -1:

                    echo "fork error : {$i} \r\n";

                    exit;

                case 0:

                    $param = array(

                        &#39;lastid&#39; => $max / $workers * $i,

                        'maxid' => $max / $workers * ($i+1),

                    );

                    $this->doSomething($pid, $param);

                    exit;

                default:

                    break;

            }

        }

        

        foreach ($pids as $i => $pid) {

            if($pid) {

                //父进程会得到子进程号,所以这里是父进程执行的逻辑  

                //如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成:  

                pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。

                //pcntl_waitpid($pid, $status);

            }

        }

    }



// 可以看到那个子进程在处理哪些数据

    public function doSomething($filename, $param)

    {

        for ($i = $param['lastid']; $i < $param['maxid']; $i++) {

            $path = "./test/";

            if (!is_dir($path)){

                mkdir($path, 0777, true);

            }
            $content = $i . '|';
            $file = $path . $filename . ".txt";
            file_put_contents($file, $content, FILE_APPEND);
        }
    }
Copy after login

More suggestions are to read documents, processes and threads, there is a lot of content, you need to read and learn

The above is the detailed content of Introduction to the usage of PHP's Pcntl multi-process extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template