In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use multi-process to deal with tasks in PHP? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
PHP multiprocess task pcntl module (this module is not supported on non-Unix systems)
A simple example of PHP multiple processes looks something like this:
/ / 5 child processes processing task for ($I = 0; $I
< 5; $i++) {$pid = pcntl_fork();if ($pid == -1) {die("could not fork");} elseif ($pid) {echo "I'm the Parent $i\n";} else { // 子进程处理 echo "I'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";} 当然实际应用中我们不能够这样输出代码,不够健壮,也不够优雅,我所以找了个基于 pcntl 封装的扩展包来使用。 spatie/async - 基于 pcntl 封装的扩展包 以下是我使用 spatie/async 来优化一个多进程请求的例子 原代码(耗时 20s 左右)- https://github.com/guanguans/...: /** * @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) {$song = detail ($detail > json_decode ()-> json_decode ($detail ['json_decode]),) If (empty ($detail ['url'])) {unset ($songs [$key]);} $song = array_merge ($song, $detail);} unset ($song); return $songs;}
After improvement (takes about 4 seconds)-https://github.com/guanguans/...:
/ * @ 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 ['url_id']), true);})-> then (function ($output) use (& $songs, & $song, $key) {$song = array_merge ($song, $output); if (empty ($song [' url'])) {unset ($songs [$key]);}})-> catch (catch (\ function $function) {function ($function > catch ()) };} unset ($song); $pool- > wait (); return $songs;} this is the answer to the question about how to use multiple processes to deal with tasks in PHP. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.