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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lsswear

感谢大佬打赏 q(≧▽≦q)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值