Upgrade to Pro — share decks privately, control downloads, hide ads and more …

並列処理で消耗しているかたへ - Snidel のご紹介 -

並列処理で消耗しているかたへ - Snidel のご紹介 -

B54cc1be9e7ea6e3ff17eaf995fbe35f?s=128

Akihito Nakano

July 20, 2016
Tweet

Transcript

  1. 4OJEFMͷ͝঺հ த໺ڿਓ(.01&1"#0JOD 1)1#-5 ฒྻॲཧͰফ໣͍ͯ͠Δ͔ͨ΁

  2. 8&#ΞϓϦέʔγϣϯΤϯδχΞ άϯϚʔ த໺ڿਓ ͖͋ͬʔ !/","/0@"LJIJUP &$ࣄۀ෦Χϥʔϛʔγϣοϓάϧʔϓ IUUQBDLJOUPTIHJUIVCJP

  3. Χϥʔϛʔγϣοϓ

  4. ফ໣͍ͯ͠·ͤΜ͔ʁ

  5. None
  6. None
  7. ऴΘΒͳ͍

  8. ύϥͰ࣮ߦ͍ͨ͠

  9. Ͳ͏͢Δ wDVSM@NVMUJ wQDOUM@GPSL wQUISFBE֦ு wόοΫάϥ΢ϯυ wλεΫΩϡʔΠϯά wଞͷݴޠΛซ༻͢Δ wଞͷݴޠͰॻ͖ͳ͓͢ ʜ

  10. ΘΓͱݫ͍͠

  11. 4OJEFMͷ͝঺հ

  12. 4OJEFM εφΠσϧ IUUQTHJUIVCDPNBDLJOUPTITOJEFM खܰʹϚϧνϓϩηεͰ͖Δ ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ ϚελʔʗϫʔΧʔϞσϧ 1)1Ҏ্

  13. खܰʹϚϧνϓϩηεͰ͖Δ

  14. खܰʹϚϧνϓϩηεͰ͖Δ $snidel = new Snidel($concurrency = 5);

  15. खܰʹϚϧνϓϩηεͰ͖Δ $snidel = new Snidel($concurrency = 5); $sleep = function

    () { sleep(3); };
  16. खܰʹϚϧνϓϩηεͰ͖Δ $snidel = new Snidel($concurrency = 5); $sleep = function

    () { sleep(3); }; $snidel->fork($sleep); $snidel->fork($sleep);
  17. खܰʹϚϧνϓϩηεͰ͖Δ $snidel = new Snidel($concurrency = 5); $sleep = function

    () { sleep(3); }; $snidel->fork($sleep); $snidel->fork($sleep); $snidel->wait();
  18. खܰʹϚϧνϓϩηεͰ͖Δ $snidel = new Snidel($concurrency = 5); $sleep = function

    () { sleep(3); }; $snidel->fork($sleep); $snidel->fork($sleep); $snidel->wait();// 3ඵͰ͓ΘΔ
  19. खܰʹϚϧνϓϩηεͰ͖Δ // ແ໊ؔ਺ $snidel->fork($f, ‘arg'); // άϩʔόϧؔ਺ $snidel->fork('strtoupper', ‘php’); //

    Πϯελϯεϝιου $snidel->fork([$instance, ‘method’], [‘arg1’, ’arg2’]); // ελςΟοΫϝιου $snidel->fork([‘\FooClass’, ‘method’], [‘arg1’, ’arg2’]);
  20. DBMM@VTFS@GVOD@BSSBZͱ ΄΅ ಉ͡

  21. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ

  22. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; };

  23. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; }; $snidel->fork($f,

    'foo'); $snidel->fork($f, 'bar'); $snidel->fork($f, 'baz');
  24. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; }; $snidel->fork($f,

    'foo'); $snidel->fork($f, 'bar'); $snidel->fork($f, 'baz'); var_dump($snidel->get()->toArray());
  25. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; }; $snidel->fork($f,

    'foo'); $snidel->fork($f, 'bar'); $snidel->fork($f, 'baz'); var_dump($snidel->get()->toArray()); array(3) { [0] => string(3) "bar" [1] => string(3) “foo" [2] => string(3) "baz" }
  26. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; }; $snidel->fork($f,

    'foo'); $snidel->fork($f, 'bar'); $snidel->fork($f, 'baz'); var_dump($snidel->get()->toArray()); array(3) { [0] => string(3) "bar" [1] => string(3) “foo" [2] => string(3) "baz" } ˞݁Ռͷॱং͸อূ͞Εͳ͍
  27. λά෇͚͢Δ

  28. ॲཧ݁ՌΛλά෇͚͢Δ // ୈ̏Ҿ਺ʹλάΛࢦఆ͢Δ $snidel->fork($f, ‘foo’, ‘tag1’); $snidel->fork($f, ‘bar’, ‘tag1’); $snidel->fork($f,

    ‘baz’, ‘tag2’);
  29. ॲཧ݁ՌΛλά෇͚͢Δ // ୈ̏Ҿ਺ʹλάΛࢦఆ͢Δ $snidel->fork($f, ‘foo’, ‘tag1’); $snidel->fork($f, ‘bar’, ‘tag1’); $snidel->fork($f,

    ‘baz’, ‘tag2’); var_dump($snidel->get('tag1')->toArray());
  30. ॲཧ݁ՌΛλά෇͚͢Δ // ୈ̏Ҿ਺ʹλάΛࢦఆ͢Δ $snidel->fork($f, ‘foo’, ‘tag1’); $snidel->fork($f, ‘bar’, ‘tag1’); $snidel->fork($f,

    ‘baz’, ‘tag2’); var_dump($snidel->get('tag1')->toArray()); array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" }
  31. ͦͷଞͷػೳ

  32. ͦͷଞͷػೳ ϩά $fp = fopen('php://stdout', ‘w’); $snidel->setLogDestination($fp); $snidel->fork($f);

  33. ͦͷଞͷػೳ ϩά $fp = fopen('php://stdout', ‘w’); $snidel->setLogDestination($fp); $snidel->fork($f); [2016-07-20 20:00:00][info][29688(owner)]

    pid: 29688 [2016-07-20 20:00:00][info][29689(master)] pid: 29689 [2016-07-20 20:00:00][info][29688(owner)] queued task #1 [2016-07-20 20:00:00][info][29688(owner)] queued task #2 [2016-07-20 20:00:00][info][29689(master)] dequeued task #1 [2016-07-20 20:00:00][info][29689(master)] forked worker. pid: 29690
  34. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’,

    'ggg'];
  35. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’,

    'ggg']; $snidel = new Snidel($concurrency = 2);
  36. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’,

    'ggg']; $snidel = new Snidel($concurrency = 2); $camelize = $snidel->map($args, function ($arg) { /** ॲཧ1 */ return explode(' ', strtolower($arg)); })->then(function ($result) { /** ॲཧ2 */ return array_map('ucfirst', $result->getReturn()); })->then(function ($result) { /** ॲཧ3 */ return implode('', $result->getReturn()); });
  37. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’,

    'ggg']; $snidel = new Snidel($concurrency = 2); $camelize = $snidel->map($args, function ($arg) { /** ॲཧ1 */ return explode(' ', strtolower($arg)); })->then(function ($result) { /** ॲཧ2 */ return array_map('ucfirst', $result->getReturn()); })->then(function ($result) { /** ॲཧ3 */ return implode('', $result->getReturn()); }); var_dump($snidel->run($camelize));
  38. ಈ࡞Πϝʔδ IUUQTWJNFPDPN

  39. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’,

    'ggg']; $snidel = new Snidel($concurrency = 2); $camelize = $snidel->map($args, function ($arg) { /** ॲཧ1 */ return explode(' ', strtolower($arg)); })->then(function ($result) { /** ॲཧ2 */ return array_map('ucfirst', $result->getReturn()); })->then(function ($result) { /** ॲཧ3 */ return implode('', $result->getReturn()); }); var_dump($snidel->run($camelize)); ࣍ͷόʔδϣϯͰ࡟আ༧ఆ ݸผͷϥΠϒϥϦʹ
  40. ·ͱΊ

  41. ฒྻॲཧͰফ໣͍ͯ͠Δਓ͸ Snidel Λ࢖ͬͨΒ޾ͤʹͳΕΔ͔΋

  42. ϓϧϦΫΤετ׻ܴʂ IUUQTHJUIVCDPNBDLJOUPTITOJEFM

  43. ܅΋ϖύϘͰಇ͔ͳ͍͔ʁ ࠷৽ͷ࠾༻৘ใΛνΣοΫˠ !QC@SFDSVJU

  44. None