kriss/yii2-thread

使用 fsockopen 实现的 yii2 多线程

安装次数: 308

依赖: 0

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 2

开放问题: 0

类型:yii2-extension

1.1 2017-05-15 01:06 UTC

This package is auto-updated.

Last update: 2024-09-10 14:37:32 UTC


README

使用 fsockopen 模拟多线程操作

安装

推荐使用 composer.

php composer.phar require --prefer-dist kriss/yii2-thread "*"

或将以下代码添加到composer.json文件

"kriss/yii2-thread": "*"

然后使用

php composer.phar update

使用方法

1.进行配置

基本模板为 config/web.php,高级模板为对应入口的 config/main.php

示例配置如下:

'components' => [
    ...
    'thread' => [
        'class' => 'kriss\thread\components\Thread',
        'enable' => true,
        'tokenValidate' => true, // 是否开启 url token 验证,默认开启
        'tokenParam' => 'token', // token 参数名
        'token' => 'suiyitianxiezijidetoken', // 随意填写自己的验证 token
    ],
    ...
]

2.继承和修改

编写控制器来接收异步处理的链接

继承 \kriss\thread\controllers\WebThreadController 然后编写对应的 Action

3.使用

例如控制器下有个 Action 如下

public function actionSendMessage($message)
{
    Yii::info($message);
}

则在需要调用异步处理的脚本中使用:

Yii::$app->thread->addThread(['/web-thread/send-message','message'=>'hello world']);

链接['/web-thread/send-message','message'=>'hello world'] 将会在"本次请求脚本执行结束之前"(参考:register_shutdown_function) 使用fsockopen方式发起请求访问