LoginSignup
15
12

More than 3 years have passed since last update.

PHP7.4からのマルチスレッド

Last updated at Posted at 2019-06-29

PHPで並列処理と言うと、マルチプロセスではpcntl、マルチスレッドではpthreadsを使う方法が一般的です。そのpthreadsについて、PHP7.4以降のサポートの中止告知されました。PHP7から内部仕様が急速に変化しているためサポートが難しくなったようです。そこで、今後のPHPに対応できるよう新たに設計された拡張モジュールの開発が始まっています。
https://github.com/krakjoe/parallel

Parallel

Parallelはpthreadsと比べるとAPIが大きく異なります。最新のマルチスレッディングモデルを採用しているので非常に使いやすくなっています。
使用する際は7.2以降のPHPで、pthreadsと同様にZTSでコンパイルしたものが必要です。

APIに関してちょっと詳しく説明した記事を書きました。
https://qiita.com/WhiteGrouse/items/025227aff86416a1d720

↓README.mdに記載されているコードです。

<?php
$runtime = new \parallel\Runtime();

$future = $runtime->run(function(){
    for ($i = 0; $i < 500; $i++)
        echo "*";

    return "easy";
});

for ($i = 0; $i < 500; $i++) {
    echo ".";
}

printf("\nUsing \\parallel\\Runtime is %s\n", $future->value());
15
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12