添加任务
use Swoole\Coroutine;
$scheduler = new Coroutine\Scheduler;
// 添加任务
$scheduler->add(function ($a, $b) {
Coroutine::sleep(1);
echo assert($a == 'hello') . PHP_EOL;
echo assert($b == 12345) . PHP_EOL;
echo "Done.\n";
}, "hello", 12345);
// 启动协程然后执行任务
$scheduler->start();
并行任务
use Swoole\Coroutine;
$scheduler = new Coroutine\Scheduler;
// 使用10个协程执行任务
$scheduler->parallel(10, function ($t, $n) {
Coroutine::sleep($t);
echo "Co ".Coroutine::getCid()."\n";
}, 0.05, 'A');
// 启动协程然后执行任务
$scheduler->start();