异步任务投递,是将当前进程任务投递到task进程进行处理,投递结束直接返回,不对当前进程造成阻塞。方便处理耗时任务。
```php
//闭包方式投递
Task::async(function ($serv, $task_id, $data)use($post) {
$i = 0;
while ($i < 10) {
$i++;
echo $i;
//var_dump($post);
sleep(1);
}
});
//投递任务模板
$task=new \app\lib\Task($post,1);
Task::async($task);
```
模板需要继承Task接口类
如下
```php
<?php
/**
* Created by PhpStorm.
* User: xavier
* Date: 2018/8/15
* Time: 上午11:31
*/
namespace app\lib;
use xavier\swoole\Component\Task as TaskC;
class Task extends TaskC
{
public function _initialize(...$arg)
{
// TODO: Implement _initialize() method.
}
public function run($serv, $task_id, $fromWorkerId)
{
$i=0;
while($i<10){
sleep(1);
$i++;
}
}
}
```