Synergy Tasks

Synergy Task delivery provides two ways, single delivery and bulk delivery, and a single delivery is encapsulated on the basis of bulk delivery. The following collaboration tasks are delivered:

use Swoft\Task\Task;

// 协程投递
$data = Task::co('testTask', 'list', [12]);

// 协程投递
$result = Task::co('testTask', 'delete', [12]);

Task delivery

Task::co(string $name, string $method, array $params = [], float $timeout = 3, array $ext = [])

A single task is delivered, and the returned data and the task method return exactly the same type of data as the same

  • Name Delivery Task Task title
  • Method name of the method delivery task
  • The parameter passed by the params task is the parameter of the task method, and the array format is passed
  • Timeout timeout, default 3s timeout
  • The EXT task extension information is passed to the task process inside
// 任务格式
$tasks = [
    [
        'taskName',
        'method',
        ['params']
    ]
];

Task::cos(array $tasks, float $timeout = 3, array $ext = [])
  • Tasks multiple task collections, formatted above
  • Timeout timeout, default 3s timeout
  • The EXT task extension information is passed to the task process inside

Task context

Some scenes need to get the details of the task inside the task, all in the context. At this point, you can use the global function to context() get the Swoft\Task\TaskContext context object. The context provides two methods, respectively, to get Swoft\Task\Request Swoft\Task\Response all the information with the object that contains the delivery task.

$request = context()->getRequest();
$response = context()->getRespone();

Note: Be sure to get the context inside the task, otherwise you get the context of another environment

Task Request

namespace Swoft\Task;

class Request implements RequestInterface
{
   // ...
}

Method List

  • GetServer Get task Server information
  • GetTaskId get task ID, corresponding to Swoole task ID
  • The getSrcWorkerId mission comes from workerId
  • The original of the getData delivery task is data
  • GetName Task Name
  • GetMethod Task method
  • GetParams Task Parameters
  • GetExt Task Extension Information
  • GetExtKey Quick access to user information based on key
  • GetType Task type
  • GetTaskUniqid Task Global Unique ID
/docs/2.x/en/task/coroutine.html
progress-bar