SlideShare a Scribd company logo
Reactor Pattern
     and
    React
@yuya_takeyama
How to parallelize
  PHP scripts?

どうやって PHP スクリプトを並列化させるか
There is more than
     one way
     to do it!
    やり方はいろいろある
exec()
foreach (range(1, 10) as $i) {
    exec("php script.php 1>&2 &");
}
echo "Hi", PHP_EOL;
pcntl_fork()
echo getmypid() . ": I'm parent.", PHP_EOL;
foreach (range(1, 10) as $i) {
    $pid = pcntl_fork();
    if ($pid) {
        echo "{$pid}: I'm child.", PHP_EOL;
        exit;
    }
    pcntl_wait($status);
}
Reactor Pattern and React
c_ ope n()
pro
n()                         0)
       _op e                       ea m,
 p roc                       $s tr
                        ing(
                  lo ck
            et _b
      am _s
st re
n()                         0)
       _op e                       ea m,
 p roc                       $s tr
                        ing(
                  lo ck
            et _b
      am _s
st re
  strea   m_select()
n()                         0)
       _op e                       ea m,
 p roc                       $s tr
                        ing(
                  lo ck
            et _b
      am _s              popen()
st re
  strea   m_select()
n()                         0)
       _op e                       ea m,
 p roc                       $s tr
                        ing(
                  lo ck
            et _b
      am _s              popen()
st re
  strea   m_select()


                                sys tem()
n()                         0)
       _op e                       ea m,
 p roc                       $s tr
                        ing(
                     ck




                                        t()
               _b lo
         _s et




                                        c
      am                 popen()




                                    ele
st re




                                  i_s
  strea   m_select()



                               ult
                             l_m
                           cur
                                 sys tem()
n()                        0)
       _op e                      ea m,
 p roc                         tr
           Thesen oc ki
                         gare
                          ( $s




                                        t()
            t_ bl
     very se COMPLEX!




                                       c
        m_              popen()




                                   ele
   re a
st




                                  i_s
  strea   m_select()



                               ult
                            l_m
                複雑過ぎる
                          cur
                                sys tem()
Reactor Pattern and React
It’s just like...
It’s just like...
What’s

         ?
•Event-driven

• non-blocking I/O

• with Pure PHP

• Native extensions are also available
 for better performance
var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
<?php
require __DIR__ . '/vendor/autoload.php';

$loop = new ReactEventLoopStreamSelectLoop;

$socket = new ReactSocketServer($loop);
$server = new ReactHttpServer($socket);

$server->on('request', function ($req, $res) {
    $res->writeHead(200, ['Content-Type' => 'text/plain']);
    $res->end("Hello Worldn");
});

$socket->listen(1337);

echo "Server running at http://127.0.0.1:1337/", PHP_EOL;
$loop->run();
How it works?


   どうやって動くのか
The Core of React
ReactEventLoop
StreamSelectLoop
•stream_set_blocking($stream, 0)
•stream_select()
stream_set_blocking($stream, 0)


 •ストリームをブロックしない
  モードにする

 •I/O 待ちが発生しなくなる
stream_select()

 select(2) for PHP
$ man 2 select
stream_select()
•複数のストリームを監視

• 準備ができたものの数を返す

• 準備ができたものたちを配列に
セットする
複数のストリームを秒間何度も
stream_select()
で監視し、準備ができたものか
ら処理していくことで I/O 待
ちのムダを軽減
Reactor
複数のストリームを秒間何度も
stream_select()
で監視し、準備ができたものか


  Pattern
ら処理していくことで I/O 待
ちのムダを軽減
Components

•Stream    •Whois

• Socket   • ZMQ

• Http     • Predis/Async (Redis)

• Dns      • Ratchet (Web Socket)
Components

•Stream    •Whois

• Socket   • ZMQ

• Http     • Predis/Async (Redis)

• Dns      • Ratchet (Web Socket)

            ChildProcess?
Conclusion
•Reactor パターンを使うと
I/O処理を並列化/効率化できる

•
React を使うと簡単に Reactor
パターンの利点を享受できる
__halt_compiler();

More Related Content

Reactor Pattern and React