easyswoole异步任务
安装
文档地址:https://www.easyswoole.com/Components/Component/task.html
composer require easyswoole/task
异步任务案例
<?php
namespace App\HttpController;
use EasySwoole\EasySwoole\Task\TaskManager;
use EasySwoole\Http\AbstractInterface\Controller;
use Swoole\Coroutine;
use Swoole\Http\Status;
class Index extends Controller
{
    public function index()
    {
        $file = EASYSWOOLE_ROOT . '/vendor/easyswoole/easyswoole/src/Resource/Http/welcome.html';
        if (!is_file($file)) {
            $file = EASYSWOOLE_ROOT . '/src/Resource/Http/welcome.html';
        }
        $this->response()->write(file_get_contents($file));
    }
    public function async()
    {
        TaskManager::getInstance()->async($this->asyncStart(), $this->asyncFinish());
        echo "完成" . PHP_EOL;
    }
    private function asyncStart()
    {
        return function () {
            Coroutine::sleep(3);
            echo time() . "异步任务开始执行" . PHP_EOL;
        };
    }
    private function asyncFinish()
    {
        return function () {
            echo time() . "异步任务执行完成" . PHP_EOL;
        };
    }
}
启动easyswoole在浏览器访问127.0.0.1:9501/index/async
root@58d240f751b9:/var/www/easyswoole-study# php easyswoole server start
  ______                          _____                              _
 |  ____|                        / ____|                            | |
 | |__      __ _   ___   _   _  | (___   __      __   ___     ___   | |   ___
 |  __|    / _` | / __| | | | |  \___ \  \ \ /\ / /  / _ \   / _ \  | |  / _ \
 | |____  | (_| | \__ \ | |_| |  ____) |  \ V  V /  | (_) | | (_) | | | |  __/
 |______|  \__,_| |___/  \__, | |_____/    \_/\_/    \___/   \___/  |_|  \___|
                          __/ |
                         |___/
main server                   SWOOLE_WEB
listen address                0.0.0.0
listen port                   9501
worker_num                    8
reload_async                  true
max_wait_time                 3
pid_file                      /var/www/easyswoole-study/Temp/pid.pid
log_file                      /var/www/easyswoole-study/Log/swoole.log
user                          root
swoole version                4.8.12
php version                   8.1.11
easyswoole version            3.5.1
run mode                      dev
temp dir                      /var/www/easyswoole-study/Temp
log dir                       /var/www/easyswoole-study/Log
完成
1666972905异步任务开始执行
1666972905异步任务执行完成
这里特地在开始的函数里加了延迟3秒,体现出异步的功能输出。
比较推荐的还是使用任务模板
在App目录下新建一个Task里面放对应的各种异步任务类
比如:
<?php
namespace App\Task;
use EasySwoole\Task\AbstractInterface\TaskInterface;
use Swoole\Coroutine;
class TaskTemp implements TaskInterface
{
    function run(int $taskId, int $workerIndex)
    {
        // TODO: Implement run() method.
        Coroutine::sleep(1);
        echo '任务模板开始执行'.PHP_EOL;
    }
    function onException(\Throwable $throwable, int $taskId, int $workerIndex)
    {
        // TODO: Implement onException() method.
    }
}
在Index控制器中调用
<?php
namespace App\HttpController;
use App\Task\TaskTemp;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\Task\TaskManager;
use EasySwoole\Http\AbstractInterface\Controller;
use Swoole\Coroutine;
use Swoole\Http\Status;
class Index extends Controller
{
    public function index()
    {
        $file = EASYSWOOLE_ROOT . '/vendor/easyswoole/easyswoole/src/Resource/Http/welcome.html';
        if (!is_file($file)) {
            $file = EASYSWOOLE_ROOT . '/src/Resource/Http/welcome.html';
        }
        $this->response()->write(file_get_contents($file));
    }
    public function taskTemp()
    {
        TaskManager::getInstance()->async(new TaskTemp());
        echo '任务模板执行完成'.PHP_EOL;
    }
}