Skip to content

Instantly share code, notes, and snippets.

@zinntikumugai
Last active March 23, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinntikumugai/7bdaf3c1d5404b9e911562de3c7db6eb to your computer and use it in GitHub Desktop.
Save zinntikumugai/7bdaf3c1d5404b9e911562de3c7db6eb to your computer and use it in GitHub Desktop.
mpyw/Coテスト
vendor
composer.lock

動き yieldによって重い処理は実行そのものは一旦置いておいて、$taskに追加されていく Co::waitによって一斉開始 すべての処理が終了したあと(上では15秒後が)以降の処理が進む

{
"require": {
"mpyw/co": "^1.5"
}
}
<?php
require_once __DIR__ .'/vendor/autoload.php';
$task = [];
$runs = [
1,
2,
5,
10,
15,
10,
15
];
foreach($runs as $run) {
$task[] = function() use($run) {
echo "running {$run} Start" .PHP_EOL;
yield mpyw\Co\Co::DELAY => $run; //なんか知らんがsleepは対応してないらしい
echo "running {$run} Done." .PHP_EOL;
return $run;
};
}
echo "Running!" .PHP_EOL;
var_dump(mpyw\Co\Co::wait($task));
echo "End!" .PHP_EOL;
/*
動き
yieldによって重い処理は実行そのものは一旦置いておいて、$taskに追加されていく
Co::waitによって一斉開始
すべての処理が終了したあと(上では15秒後が)以降の処理が進む
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment