Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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() . ": Im parent.", PHP_EOL;foreach (range(1, 10) as $i) {    $pid = pcntl_fork();    if ($pid) ...
c_ ope n()pro
n()                         0)       _op e                       ea m, p roc                       $s tr                  ...
n()                         0)       _op e                       ea m, p roc                       $s tr                  ...
n()                         0)       _op e                       ea m, p roc                       $s tr                  ...
n()                         0)       _op e                       ea m, p roc                       $s tr                  ...
n()                         0)       _op e                       ea m, p roc                       $s tr                  ...
n()                        0)       _op e                      ea m, p roc                         tr           Thesen oc ...
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...
<?phprequire __DIR__ . /vendor/autoload.php;$loop = new ReactEventLoopStreamSelectLoop;$socket = new ReactSocketServer($lo...
How it works?   どうやって動くのか
The Core of ReactReactEventLoop
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)            C...
Conclusion•Reactor パターンを使うとI/O処理を並列化/効率化できる•React を使うと簡単に Reactorパターンの利点を享受できる
__halt_compiler();
Reactor Pattern and React
Reactor Pattern and React
Upcoming SlideShare
Loading in …5
×

Reactor Pattern and React

6,698 views

Published on

Published in: Technology
  • Dating direct: ❶❶❶ http://bit.ly/39pMlLF ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ♥♥♥ http://bit.ly/39pMlLF ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE Format, ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

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

×