nerburish/yii2-thread

使用 fsockopen 实现的 yii2 多线程

安装: 6

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 2

类型:yii2-extension

1.0 2016-07-14 02:31 UTC

This package is not auto-updated.

Last update: 2024-09-20 19:55:40 UTC


README

使用 fsockopen 在 yii2 中模拟多线程操作

安装

推荐使用 composer.

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

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

"kriss/yii2-thread": "*"

然后使用

php composer.phar update

使用方法

1. 进行配置

basic 模板为 config/web.php, advanced 模板为对应入口的 config/main.php

示例配置如下:

'components' => [
    ...
    'thread' => [
        'class' => 'kriss\thread\components\Thread',
        'enable' => true,
        'tokenValidate' => true, // 是否开启 url 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 方式发起请求访问