hyperf 其他组件 一 Task

教程:Hyperf

为防止有些函数导致堵塞进程,使用task模拟携程处理。本质上是多进程运行阻塞函数,性能取决于Task Worker数量。

这块的实现我没看懂。

一、安装

composer require hyperf/task

 二、配置

Task不是默认配置,须在server.php中加Task配置。

#config/autoload/server.php 
'settings' => [
        ……
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 2,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,
],
'callbacks' => [
        ……
        // Task callbacks
        SwooleEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        SwooleEvent::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
],

三、使用

1、主动方法投递

namespace App\Task;

use Hyperf\Utils\Coroutine;

//主动投递方式
class MethodTask
{
    public function handle($cid)
    {
        return [
            'worker.cid' => $cid,
            // task_enable_coroutine 为 false 时返回 -1,反之 返回对应的协程 ID
            'task.cid' => Coroutine::id(),
        ];
    }
}

#App\Controller\TestController
public function test4()
    {
        $container = ApplicationContext::getContainer();
        $exec = $container->get(TaskExecutor::class);
        $result = $exec->execute(new Task([MethodTask::class, 'handle'], [Coroutine::id()]));
        var_dump($result);
    }


#输出
array(2) {
  ["worker.cid"]=>
  int(4)
  ["task.cid"]=>
  int(-1)
}

#Hyperf\Task\TaskExecutor
public function execute(Task $task, float $timeout = 10)
    {
        if (!$this->server instanceof Server) {
            throw new TaskExecuteException('The server does not support task.');
        }
        $taskId = $this->server->task($task, $task->workerId);
        if ($taskId === false) {
            throw new TaskExecuteException('Task execute failed.');
        }

        $result = $this->factory->pop($taskId, $timeout);

        if ($result instanceof Exception) {
            $exception = $this->normalizer->denormalize($result->attributes, $result->class);
            if ($exception instanceof \Throwable) {
                throw $exception;
            }

            throw new TaskExecuteException(get_class($exception) . ' is not instance of Throwable.');
        }

        return $result;
    }


#vendor\swoole\ide-helper\src\swoole\Swoole\Server.php
  public function task($data, $worker_id = null, ?callable $finish_callback = null)
    {
    }

 其他地方也没找到task对应的方法实现……

2、使用注释

namespace App\Task;

use Hyperf\Task\Annotation\Task;
use Hyperf\Utils\Coroutine;

class AnnotationTask
{
    /**
     * @Task
     */
    public function handle($cid)
    {
        return [
            'worker.cid' => $cid,
            // task_enable_coroutine=false 时返回 -1,反之 返回对应的协程 ID
            'task.cid' => Coroutine::id(),
        ];
    }
}

#App\Controller\TestController
public function test5()
    {
        $container = ApplicationContext::getContainer();
        $task = $container->get(AnnotationTask::class);
        $result = $task->handle(Coroutine::id());
        var_dump($result);
    }


#输入内容
array(2) {
  ["worker.cid"]=>
  int(2)
  ["task.cid"]=>
  int(-1)
}
namespace App\Task;
use Hyperf\Utils\Coroutine;

class AnnotationTask
{
    public function handle($cid)
    {
        return [
            'worker.cid' => $cid,
            // task_enable_coroutine=false 时返回 -1,反之 返回对应的协程 ID
            'task.cid' => Coroutine::id(),
        ];
    }
}

#App\Controller\TestController
public function test5()
    {
        $container = ApplicationContext::getContainer();
        $task = $container->get(AnnotationTask::class);
        $result = $task->handle(Coroutine::id());
        var_dump($result);
    }


#输出内容
array(2) {
  ["worker.cid"]=>
  int(2)
  ["task.cid"]=>
  int(2)
}

 最后这种是错误使用,应该没关联到server。

你可能感兴趣的:(php,php)