Home > Backend Development > PHP Tutorial > Take a look at PHP multi-process processing tasks

Take a look at PHP multi-process processing tasks

coldplay.xixi
Release: 2023-04-09 15:44:01
forward
4643 people have browsed it

Take a look at PHP multi-process processing tasks

pcntl Module (non-Unix systems do not support this module)

A simple example of PHP multi-process probably looks like this:

// 5 个子进程处理任务for ($i = 0; $i < 5; $i++) {
    $pid = pcntl_fork();    if ($pid == -1) {        die("could not fork");
    } elseif ($pid) {        echo "I&#39;m the Parent $i\n";
    } else { // 子进程处理
        echo "I&#39;m the Child $i\n";        // 业务处理
        exit($i); // 一定要注意退出子进程,否则 pcntl_fork() 会被子进程再 fork,带来处理上的影响。
    }
}// 等待子进程执行结束while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);    echo "Child $status completed\n";
}复制代码
Copy after login

Of course, in actual applications, we cannot output code like this. It is not robust enough and elegant enough, so I found an extension package based on pcntl encapsulation to use.

spatie/async - An extension package based on pcntl

The following is an example of how I use spatie/async to optimize a multi-process request

Original code (takes about 20s) - github.com/guanguans/m…

Take a look at PHP multi-process processing tasks

/**
 * @param string $keyword
 *
 * @return array
 */public function searchAll(string $keyword): array{
    $songAll = [];    foreach ($this->platforms as $platform) {
        $songAll = array_merge($songAll, $this->search($platform, $keyword));
    }    return $songAll;
}/**
 * @param string $platform
 * @param string $keyword
 *
 * @return mixed
 */public function search(string $platform, string $keyword){
    $meting = $this->getMeting($platform);

    $songs = json_decode($meting->format()->search($keyword), true);    foreach ($songs as $key => &$song) {
        $detail = json_decode($meting->format()->url($song[&#39;url_id&#39;]), true);        if (empty($detail[&#39;url&#39;])) {            unset($songs[$key]);
        }
        $song = array_merge($song, $detail);
    }    unset($song);    return $songs;
}复制代码
Copy after login

After improvement (takes about 4s) - github.com /guanguans/m…

Take a look at PHP multi-process processing tasks

/**
 * @param string $keyword
 *
 * @return array
 */public function searchAll(string $keyword): array{
    $songAll = [];
    $pool = Pool::create();    foreach ($this->platforms as $platform) {
        $pool->add(function () use ($platform, $keyword) {            return $this->search($platform, $keyword);
        }, $this->getSerializedOutput())->then(function ($output) use (&$songAll) {
            $songAll = array_merge($songAll, $output);
        })->catch(function (\Throwable $exception) {            exit($exception->getMessage());
        });
    }
    $pool->wait();    return $songAll;
}/**
 * @return mixed
 */public function search(string $platform, string $keyword){
    $meting = $this->getMeting($platform);
    $songs = json_decode($meting->format()->search($keyword), true);

    $pool = Pool::create();    foreach ($songs as $key => &$song) {
        $pool->add(function () use ($meting, $song) {            return json_decode($meting->format()->url($song[&#39;url_id&#39;]), true);
        })->then(function ($output) use (&$songs, &$song, $key) {
            $song = array_merge($song, $output);            if (empty($song[&#39;url&#39;])) {                unset($songs[$key]);
            }
        })->catch(function (\Throwable $exception) {            exit($exception->getMessage());
        });
    }    unset($song);
    $pool->wait();    return $songs;
}复制代码
Copy after login

If you want to learn more about programming, please pay attention tophp trainingColumn!

The above is the detailed content of Take a look at PHP multi-process processing tasks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
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