Coroutine task

Coroutine task delivery provides two ways, single delivery and bulk delivery, and single delivery is encapsulated on a batch delivery basis. The following coroutine task delivery (note: the third parameter of the co/async method is passed as an array. When the parameter declared by the task is an array, the third parameter of co/async needs to be [[xxx,xxx,xxx]] ):

 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 = []) 

Single task delivery, return data and task method return the exact same type of data

  • Name posting task name
  • Method name of the delivery task
  • The parameters passed by the params task are the parameters of the task method, and the array format is passed.
  • Timeout timeout, default 3s timeout
  • Ext task extension information, will be passed to the task process
 // 任务格式
$tasks = [
    [
        'taskName',
        'method',
        ['params']
    ]
];

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

Task context

Some scenes need to get the details of the task in the task, all of which are in the context. At this point you can use the global function context() get the Swoft\Task\TaskContext context object. The context provides two methods for getting the Swoft\Task\Request and Swoft\Task\Response objects, which contain all the information about the delivery task.

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

Note: Be sure to get the context in the task, otherwise you will get the context of other environments.

Task Request

 namespace Swoft\Task;

class Request implements RequestInterface
{
   // ...
} 

Method list

  • getServer Get task Server information
  • getTaskId Get the task ID, corresponding to the Swoole task ID
  • getSrcWorkerId The workerId from which the task came from
  • The raw data of the getData delivery task is the data.
  • getName task name
  • getMethod task method
  • getParams task parameters
  • getExt task extension information
  • getExtKey Get user information quickly based on key
  • getType task type
  • getTaskUniqid task global unique ID
/docs/2.x/en/task/coroutine.html
progress-bar