Reactor Pattern and React

6,240 views

Published on

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,240
On SlideShare
0
From Embeds
0
Number of Embeds
1,627
Actions
Shares
0
Downloads
26
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Reactor Pattern and React

  1. 1. Reactor Pattern and React@yuya_takeyama
  2. 2. How to parallelize PHP scripts?どうやって PHP スクリプトを並列化させるか
  3. 3. There is more than one way to do it! やり方はいろいろある
  4. 4. exec()foreach (range(1, 10) as $i) {    exec("php script.php 1>&2 &");}echo "Hi", PHP_EOL;
  5. 5. pcntl_fork()echo getmypid() . ": Im parent.", PHP_EOL;foreach (range(1, 10) as $i) {    $pid = pcntl_fork();    if ($pid) {        echo "{$pid}: Im child.", PHP_EOL;        exit;    }    pcntl_wait($status);}
  6. 6. c_ ope n()pro
  7. 7. n() 0) _op e ea m, p roc $s tr ing( lo ck et _b am _sst re
  8. 8. n() 0) _op e ea m, p roc $s tr ing( lo ck et _b am _sst re strea m_select()
  9. 9. n() 0) _op e ea m, p roc $s tr ing( lo ck et _b am _s popen()st re strea m_select()
  10. 10. 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()
  11. 11. n() 0) _op e ea m, p roc $s tr ing( ck t() _b lo _s et c am popen() elest re i_s strea m_select() ult l_m cur sys tem()
  12. 12. 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 ast i_s strea m_select() ult l_m 複雑過ぎる cur sys tem()
  13. 13. It’s just like...
  14. 14. It’s just like...
  15. 15. What’s ?
  16. 16. •Event-driven• non-blocking I/O• with Pure PHP• Native extensions are also available for better performance
  17. 17. 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/);
  18. 18. <?phprequire __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();
  19. 19. How it works? どうやって動くのか
  20. 20. The Core of ReactReactEventLoop
  21. 21. StreamSelectLoop
  22. 22. •stream_set_blocking($stream, 0)•stream_select()
  23. 23. stream_set_blocking($stream, 0) •ストリームをブロックしない モードにする •I/O 待ちが発生しなくなる
  24. 24. stream_select() select(2) for PHP
  25. 25. $ man 2 select
  26. 26. stream_select()•複数のストリームを監視• 準備ができたものの数を返す• 準備ができたものたちを配列にセットする
  27. 27. 複数のストリームを秒間何度もstream_select()で監視し、準備ができたものから処理していくことで I/O 待ちのムダを軽減
  28. 28. Reactor複数のストリームを秒間何度もstream_select()で監視し、準備ができたものか Patternら処理していくことで I/O 待ちのムダを軽減
  29. 29. Components•Stream •Whois• Socket • ZMQ• Http • Predis/Async (Redis)• Dns • Ratchet (Web Socket)
  30. 30. Components•Stream •Whois• Socket • ZMQ• Http • Predis/Async (Redis)• Dns • Ratchet (Web Socket) ChildProcess?
  31. 31. Conclusion•Reactor パターンを使うとI/O処理を並列化/効率化できる•React を使うと簡単に Reactorパターンの利点を享受できる
  32. 32. __halt_compiler();

×