34
Reactor Pattern and React @yuya_takeyama

Reactor Pattern and React

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Reactor Pattern and React

Reactor Patternand

React@yuya_takeyama

Page 2: Reactor Pattern and React

How to parallelizePHP scripts?

どうやって PHP スクリプトを並列化させるか

Page 3: Reactor Pattern and React

There is more thanone wayto do it!やり方はいろいろある

Page 4: Reactor Pattern and React

exec()

foreach (range(1, 10) as $i) {    exec("php script.php 1>&2 &");}echo "Hi", PHP_EOL;

Page 5: Reactor Pattern and React

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);}

Page 6: Reactor Pattern and React
Page 7: Reactor Pattern and React

proc_open()

Page 8: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

proc_open()

Page 9: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

proc_open()

stream_select()

Page 10: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

proc_open()

popen()

stream_select()

Page 11: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

proc_open()

popen()

system()

stream_select()

Page 12: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

curl_multi_select()

proc_open()

popen()

system()

stream_select()

Page 13: Reactor Pattern and React

stream

_set_b

lockin

g($str

eam, 0

)

curl_multi_select()

proc_open()

popen()

system()

stream_select()

These arevery COMPLEX!

複雑過ぎる

Page 14: Reactor Pattern and React
Page 15: Reactor Pattern and React

It’s just like...

Page 16: Reactor Pattern and React

It’s just like...

Page 17: Reactor Pattern and React

What’s

?

Page 18: Reactor Pattern and React

• Event-driven

• non-blocking I/O

• with Pure PHP

• Native extensions are also availablefor better performance

Page 19: Reactor Pattern and React

var http = require('http');

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

console.log('Server running at http://127.0.0.1:1337/');

Page 20: Reactor Pattern and React

<?phprequire __DIR__ . '/vendor/autoload.php';

$loop = new \React\EventLoop\StreamSelectLoop;

$socket = new \React\Socket\Server($loop);$server = new \React\Http\Server($socket);

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

$socket->listen(1337);

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

Page 21: Reactor Pattern and React

How it works?

どうやって動くのか

Page 22: Reactor Pattern and React

\React\EventLoop

The Core of React

Page 23: Reactor Pattern and React

StreamSelectLoop

Page 24: Reactor Pattern and React

•stream_set_blocking($stream, 0)

•stream_select()

Page 25: Reactor Pattern and React

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

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

stream_set_blocking($stream, 0)

Page 26: Reactor Pattern and React

stream_select()

select(2) for PHP

Page 27: Reactor Pattern and React

$ man 2 select

Page 28: Reactor Pattern and React

•複数のストリームを監視•準備ができたものの数を返す•準備ができたものたちを配列にセットする

stream_select()

Page 29: Reactor Pattern and React

複数のストリームを秒間何度もstream_select()

で監視し、準備ができたものから処理していくことで I/O 待ちのムダを軽減

Page 30: Reactor Pattern and React

複数のストリームを秒間何度もstream_select()

で監視し、準備ができたものから処理していくことで I/O 待ちのムダを軽減

ReactorPattern

Page 31: Reactor Pattern and React

• Stream

• Socket

• Http

• Dns

Components

• Whois

• ZMQ

• Predis/Async (Redis)

• Ratchet (Web Socket)

Page 32: Reactor Pattern and React

• Stream

• Socket

• Http

• Dns

Components

• Whois

• ZMQ

• Predis/Async (Redis)

• Ratchet (Web Socket)

• ChildProcess?

Page 33: Reactor Pattern and React

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

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

Conclusion

Page 34: Reactor Pattern and React

__halt_compiler();