PHP code example of qbhy / swoole-parallel

1. Go to this page and download the library: Download qbhy/swoole-parallel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qbhy / swoole-parallel example snippets




\Swoole\Coroutine::create(function () {
    $time = time();
    $parallel = new \Qbhy\SwooleParallel\Parallel(20);

//    $parallel->setCaller(function ($callback) {
//        return app()->call($callback); // laravel 内可以这样实现协程内依赖注入
//    });

    for ($i = 0; $i < 5; ++$i) {
        $parallel->add(function () use ($i) {
            \Swoole\Coroutine::sleep(1);
            return $i;
        });
    }

    $results = $parallel->wait();
    $diff = time() - $time;
    var_dump($results);
    print_r("运行了:{$diff}秒");
});