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

    View full-size slide

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

    View full-size slide

  3. Χϥʔϛʔγϣοϓ

    View full-size slide

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

    View full-size slide

  5. ύϥͰ࣮ߦ͍ͨ͠

    View full-size slide

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

    View full-size slide

  7. ΘΓͱݫ͍͠

    View full-size slide

  8. 4OJEFMͷ͝঺հ

    View full-size slide

  9. 4OJEFM εφΠσϧ

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  16. खܰʹϚϧνϓϩηεͰ͖Δ
    // ແ໊ؔ਺
    $snidel->fork($f, ‘arg');
    // άϩʔόϧؔ਺
    $snidel->fork('strtoupper', ‘php’);
    // Πϯελϯεϝιου
    $snidel->fork([$instance, ‘method’], [‘arg1’, ’arg2’]);
    // ελςΟοΫϝιου
    $snidel->fork([‘\FooClass’, ‘method’], [‘arg1’, ’arg2’]);

    View full-size slide

  17. DBMM@VTFS@GVOD@BSSBZͱ ΄΅
    ಉ͡

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ
    $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"
    }

    View full-size slide

  23. ࢠϓϩηεͷॲཧ݁ՌΛड͚औΕΔ
    $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"
    }
    ˞݁Ռͷॱং͸อূ͞Εͳ͍

    View full-size slide

  24. λά෇͚͢Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. ॲཧ݁ՌΛλά෇͚͢Δ
    // ୈ̏Ҿ਺ʹλάΛࢦఆ͢Δ
    $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"
    }

    View full-size slide

  28. ͦͷଞͷػೳ

    View full-size slide

  29. ͦͷଞͷػೳ ϩά

    $fp = fopen('php://stdout', ‘w’);
    $snidel->setLogDestination($fp);
    $snidel->fork($f);

    View full-size slide

  30. ͦͷଞͷػೳ ϩά

    $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

    View full-size slide

  31. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ

    $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’, 'ggg'];

    View full-size slide

  32. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ

    $args = ['aaa', ‘bbb’, ‘ccc’, ‘ddd’, ‘eee’, ‘fff’, 'ggg'];
    $snidel = new Snidel($concurrency = 2);

    View full-size slide

  33. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ

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

    View full-size slide

  34. ͦͷଞͷػೳ ͭͳ࣮͛ͯߦ

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

    View full-size slide

  35. ಈ࡞Πϝʔδ
    IUUQTWJNFPDPN

    View full-size slide

  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());
    });
    var_dump($snidel->run($camelize));
    ࣍ͷόʔδϣϯͰ࡟আ༧ఆ
    ݸผͷϥΠϒϥϦʹ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide