OpenSwoole Server task()

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

Declaration

<?php OpenSwoole\Server->task(mixed $data, int $dstWorkerId = -1, callable $finishCallback = null): int

Parameters

data

The data which to send to the task worker, must be a serializable PHP variable

dstWorkerId

The ID number of the task worker. If this parameter hasn't been passed, the swoole server would select a random and unoccupied task worker process for you.

finishCallback

The callback function that is called when the task has been finished. If this parameter has been passed, there is no need to register the callback function of the event of onFinish

Return

success

If success, it returns the task ID of which process accepted it, otherwise it returns false.

Description

Send data to a task worker processes.

Start a new asynchronous task. This function is non-blocking and will return immediately after sending the task to the pool of task workers. The current worker process can then continue to process new requests.

To call this method, you need to set the configuration of task_worker_num and the callback function of onTask and onFinish.

Things to Consider

  • This function is used to execute slow tasks asynchronously, such as a chat server, which can be used to send broadcasts. When the task is completed, the task process calls $server->finish("finish") telling the worker process this task has been completed. Of course, OpenSwoole\Server->finish is optional. The task can then operate without blocking the main server from processing new requests.

  • The task worker design uses UnixSocket to communication, when the memory is full, no I/O is used. Tasks have huge Read and write performance of up to a single process being able to handle around 100k, more processes using different UnixSocket communication pipes, can be maximized by using more CPU cores.

  • If the task worker is not a specified, OpenSwoole determines which task processes are in a busy state, OpenSwoole will only select idle Task processes to deliver new tasks to. If all Task processes are busy, OpenSwoole will poll and deliver tasks to each process. You can use the $server->stats method to get the number of tasks currently queued and waiting, this results in no disk I/O being used.

  • The third parameter (finishCallback) can be set to avoid any event triggers for onFinish, if the task has a set callback function then the task system executes the specified callback function and any results are returned, no longer performing the Server onFinish event.

  • The data that is sent to a task is usually best not too exceed 8k, if this does occur then OpenSwoole will temporarily save the data to disk while waiting for the task system to use the data that is queued. If the temporarily saved data on disk exceeds package_max_length, OpenSwoole will then start to throw warnings but, this is not an error, it just means you may experience performance issues with the server.

  • Task worker processes can not be used by user defined processes, instead you can use sendMessage to communicate between them.

  • Inside a task worker process, you should not return data using return otherwise $Server->finish() will output the result.

Example

Using the task finish callback

<?php
$task_id = $server->task("some data");

$server->task("taskcallback", -1, function (OpenSwoole\Server $server, $task_id, $data)
{
    echo "Task Callback: ";
    var_dump($task_id, $data);
});

General server task usage

<?php
$server = new OpenSwoole\Server("127.0.0.1", 9501, OpenSwoole\Server::SIMPLE_MODE);

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

$server->on('Receive', function (OpenSwoole\Server $server, $fd, $reactorId, $data)
{
    echo "Received data: " . $data . "\n";
    $data    = trim($data);

    $server->task($data, -1, function (OpenSwoole\Server $server, $task_id, $data)
    {
        echo "Task Callback: ";
        var_dump($task_id, $data);
    });

    $task_id = $server->task($data, 0);

    $server->send($fd, "New task started with id: $task_id\n");
});

$server->on('Task', function (OpenSwoole\Server $server, $task_id, $reactorId, $data)
{
    echo "Task Worker Process received data";

    echo "#{$server->worker_id}\tonTask: [PID={$server->worker_pid}]: task_id=$task_id, data_len=" . strlen($data) . "." . PHP_EOL;

    $server->finish($data);
});

$server->on('Finish', function (OpenSwoole\Server $server, $task_id, $data)
{
    echo "Task#$task_id finished, data_len=" . strlen($data) . PHP_EOL;
});

$server->on('workerStart', function ($server, $worker_id)
{
    if($worker_id >= $server->setting['worker_num'])
    {
        OpenSwoole\Util::setProcessName("php {$argv[0]}: task_worker");
    }
    else
    {
        OpenSwoole\Util::setProcessName("php {$argv[0]}: worker");
    }
});

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