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

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

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

Akihito Nakano

July 20, 2016
Tweet

More Decks by Akihito Nakano

Other Decks in Programming

Transcript

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

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

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

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

    Πϯελϯεϝιου $snidel->fork([$instance, ‘method’], [‘arg1’, ’arg2’]); // ελςΟοΫϝιου $snidel->fork([‘\FooClass’, ‘method’], [‘arg1’, ’arg2’]);
  5. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $f = function ($arg) { return $arg; }; $snidel->fork($f,

    'foo'); $snidel->fork($f, 'bar'); $snidel->fork($f, 'baz'); var_dump($snidel->get()->toArray());
  6. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $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" }
  7. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ $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" } ˞݁Ռͷॱং͸อূ͞Εͳ͍
  8. ॲཧ݁ՌΛλά෇͚͢Δ // ୈ̏Ҿ਺ʹλάΛࢦఆ͢Δ $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" }
  9. ͦͷଞͷػೳ ϩά $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
  10. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $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()); });
  11. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $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));
  12. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ $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)); ࣍ͷόʔδϣϯͰ࡟আ༧ఆ ݸผͷϥΠϒϥϦʹ