際際滷

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

どうやって PHP スクリプトをK双晒させるか
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
                }j^ぎる
                          cur
                                sys tem()
Reactor Pattern and React
It¨s just like...
It¨s just like...
安鞄温岳¨壊

         ?
?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 棋ちがk伏しなくなる
stream_select()

 select(2) for PHP
$ man 2 select
stream_select()
?}方のストリ`ムをO

? 笋できたものの方を卦す

? 笋できたものたちを塘双に
セットする
}方のストリ`ムを昼g採業も
stream_select()
でOし、笋できたものか
らI尖していくことで I/O 棋
ちのムダをXp
Reactor
}方のストリ`ムを昼g採業も
stream_select()
でOし、笋できたものか


  Pattern
らI尖していくことで I/O 棋
ちのムダをXp
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/OI尖をK双晒/紳併できる

?
React を聞うとgに Reactor
パタ`ンの旋泣を輅椶任る
__halt_compiler();

More Related Content

Reactor Pattern and React