OpenSwoole Server taskCo()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Server->taskCo(array $tasks, float $timeout = 0.5): array

Parameters

tasks

An array of data which will be sent to a number of task workers to process each element of the array. Each element will be a new task and each task will be executed concurrently. The array must be numerically indexed, no associated arrays.

timeout

The amount of time in seconds to wait for the tasks to complete, this function won't return until all the tasks are completed or if this timeout is reached, if the timeout is reached, false will be returned when no tasks complete in time. Minimum value is 1ms. Default is 0.5s.

Return

array

As multiple tasks are executed based on a numerically indexed array, the result will be a numerically indexed array in the same order of the data. So $tasks[2] will have its result at $result[2]. If the timeout is reached, false is set for the value of the task data but a timeout will not affect other tasks which have completed in time, you must always check the return results for a false value.

Description

Concurrently execute a set of tasks based on a numerically indexed array where each element of data will be a new task without blocking the main process. The returned results will match up to the order of the data array which was passed, a task failure or timeout will deem the result value false, so if $tasks[2] fails or times out, $results[2] will be `false.

Each task list will be randomly delivered to a Task worker process, once the task delivery is complete, the current coroutine can be suspended so the main process won't be blocked, meaning the current worker process can accept new requests until the set of tasks return their results to continue. A timer is used to pause the client while the process is waiting for the task results, if you reach the timeout and not all the results are finished, those tasks are marked with false, so you must always check the returned results to see what completed in time. The coroutine resume is done automatically for you by OpenSwoole if the timeout is reached but OpenSwoole will continue to check if all results are returned and wait again (while under the timeout) if they are not.

The maximum number of concurrent tasks must not exceed 1024 in your array of data. If the task workers are all occupied, any new tasks are queued up and will wait until a task finishes before starting a new one.

Example

<?php
$server = new OpenSwoole\Http\Server("0.0.0.0", 9501, OpenSwoole\Server::POOL_MODE);

$server->set([
    'worker_num' => 1,
    'task_worker_num' => 2,
]);

$server->on('Request', function ($request, $response) use ($server)
{
    $tasks[0] = ['time' => 0];
    $tasks[1] = ['data' => 'openswoole.com', 'code' => 200];

    $result = $server->taskCo($tasks, 1.5);

    $response->end('Task Result: ' . var_export($result, true));
});

$server->on('Task', function (OpenSwoole\Server $server, $task_id, $worker_id, $data)
{
    if ($server->worker_id == 1)
    {
        sleep(1);
    }

    $data['done'] = time();
    $data['worker_id'] = $server->worker_id;

    return $data;
});

$server->start();
Last updated on September 20, 2022