LoginSignup
4
4

More than 5 years have passed since last update.

PHPで並列処理を作ってみた

Last updated at Posted at 2016-01-10

今回やる方法はPHPだけでやるわけではないのでご了承ください。PHPだけでやる方法はまた今度書くかもしれません。因みに、今回使ったOSはCentOS6です。
処理をexsample.phpに書き、test.phpから呼び出します。

test.php
    $file = __DIR__.'/example.php'; //処理を書いたファイル
    foreach($datas AS $data){
        exec("/usr/bin/php $file -d ".escapeshellarg($data)." > /dev/null &");
    }
example.php
    function test($data){
    //なんか$data使った処理をここに
    }
    test(getopt("d"))

execで外部コマンドを使うので、OSなどの環境に合わせて書き換えてください。Linuxではおなじみの> /dev/null &で結果を返さないようにすることで、結果を待たずに次の処理を実行します。因みにescapeshellargはシェル引数にデータを渡すための安全な引数を作成します。なので、渡すデータが数字だけの場合とかは書かなくていいと思うけど…まあ一応ね?

4
4
2

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
4
4