Coroutine runner is similar to CSP component, but it is more flexible. It can post the tasks of coroutine and limit the maximum number of simultaneous execution, the maximum execution time, success or failure callback

Simple example

use co;
use EasySwoole\Component\CoroutineRunner\Runner;
use EasySwoole\Component\CoroutineRunner\Task;

$runner = new Runner(10);
$runner->setOnException(function(\Throwable  $e, $task){
    echo $e->getMessage();
    echo PHP_EOL;

$max = 30;
$allTask = [];
    $task = new Task(function() use ($max) {
        echo $max .PHP_EOL;
        // It will be assumed that this is a curl crawling task. Return crawling results can be obtained externally
        return 'ok';
    $allTask[] = $task;

$runner->start(1);// The longest execution time is 1 second, 30 posts are posted in total, and the maximum concurrent execution time is 10, which takes 3 seconds to complete, so some of them will be discarded. See the parameter description list below

foreach($allTask as $key => $task){

Parameter description

Runner constructor

Two parameters can be received __construct($concurrency = 64,$taskChannelSize = 1024)

  • concurrency Maximum number of concurrent processes
  • taskChannelSize Deliverable task queue length


There are two parameters for setting exception callback (\Throwable $e, Task $task)


To start the execution of the delivered task process, there is a parameter

  • float $waitTime = 30 The maximum execution time. If this time is exceeded, the remaining tasks will be discarded and will not be executed.

Task constructor

You need a callable parameter to call execution. You can use '$task - > getresult()' to get the return data in the 'closure' and external use '$task - > getresult()'

  • return Data not equal to false will trigger onsuccess
  • return False will trigger onfail


A call parameter is required

[not required] callback after task execution


A call parameter is required

[not required] callback for task execution failure


Get the return data after the call function is executed