首頁  >  文章  >  php框架  >  workerman如何搭配tp

workerman如何搭配tp

(*-*)浩
(*-*)浩原創
2019-12-05 11:08:072633瀏覽

workerman如何搭配tp

下載workerman,放入thinkphp,注意與Home平級,就把workerman當做是一個模組。進入Home/Controller目錄,新建WorkermanController.class.php。        (建議學習: workerman教學

#直接上原始碼,這裡我是以守護程式方式運作的,除錯的話,可以去掉daemonize = true 這行。

<?php
namespace Home\Controller;
use Workerman\Worker;
/**
 * 用户信息查询
 */
class WorkermanController{
    /**
    * 用户信息查询
    */
    public function index(){
        if(!IS_CLI){
            die("access illegal");
        }
        require_once APP_PATH.&#39;Workerman/Autoloader.php&#39;;
        define(&#39;MAX_REQUEST&#39;, 1000);// 每个进程最多执行1000个请求
        Worker::$daemonize = true;//以守护进程运行
        Worker::$pidFile = &#39;/data/wwwlogs/Worker/workerman.pid&#39;;//方便监控WorkerMan进程状态
        Worker::$stdoutFile = &#39;/data/wwwlogs/Worker/stdout.log&#39;;//输出日志, 如echo,var_dump等
        Worker::$logFile = &#39;/data/wwwlogs/Worker/workerman.log&#39;;//workerman自身相关的日志,包括启动、停止等,不包含任何业务日志
        $worker = new Worker(&#39;text://172.16.0.10:10024&#39;);//此处我使用内网ip
        $worker->name = &#39;Worker&#39;;
        $worker->count = 2;
        //$worker->transport = &#39;udp&#39;;// 使用udp协议,默认TCP
        $worker->onWorkerStart = function($worker){
            echo "Worker starting...\n";
        };
        $worker->onMessage = function($connection, $data){
            static $request_count = 0;// 已经处理请求数
            //$_rs=D("Article")->gettest();
            $_articleObj=A("article");
            $_rs=$_articleObj->gettest();
            var_dump($_rs);
            $connection->send("hello");
            /*
             * 退出当前进程,主进程会立刻重新启动一个全新进程补充上来,从而完成进程重启
             */
            if(++$request_count >= MAX_REQUEST){// 如果请求数达到1000
                Worker::stopAll();
            }
        };
        $worker->onBufferFull = function($connection){
            echo "bufferFull and do not send again\n";
        };
        $worker->onBufferDrain = function($connection){
            echo "buffer drain and continue send\n";
        };
        $worker->onWorkerStop = function($worker){
            echo "Worker stopping...\n";
        };
        $worker->onError = function($connection, $code, $msg){
            echo "error $code $msg\n";
        };
        // 运行worker
        Worker::runAll();
    }
}

修改Workerman/Worker.php原始碼,找到parseCommand()方法,workerman版本3.3.2的話,在586行,修改命令列偵測語法:

<?php
protected static function parseCommand()
{
    global $argv;
    // Check argv;
    $start_file = $argv[0];
    if (!isset($argv[2])) { //修改了此处
       exit("Usage: php yourfile.php Controller/Action {start|stop|restart|reload|status|kill}\n");//修改了此处提示
    }
    // Get command.
    $command  = trim($argv[2]);//修改了此处
    $command2 = isset($argv[3]) ? $argv[3] : &#39;&#39;;//修改了此处
   ....
}

OK,此時大功告成。

Linux命令列下運行,注意,此處要切換到thinkphp根目錄下面

/usr/local/php/bin/php index.php Workerman/index start

#查看運行狀態:

/usr/local/php/bin/php index.php Workerman/index status

以上是workerman如何搭配tp的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn