search
HomePHP FrameworkWorkermanHow to match workerman with tp

How to match workerman with tp

Dec 05, 2019 am 11:08 AM
workerman

How to match workerman with tp

Download workerman, put it into thinkphp, make sure it is at the same level as Home, and treat workerman as a module. Enter the Home/Controller directory and create a new WorkermanController.class.php.​ (Recommended study: workerman tutorial)

Go directly to the source code. Here I run it in daemon mode. For debugging, you can remove the line 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();
    }
}

Modify the Workerman/Worker.php source code and find the parseCommand() method. For workererman version 3.3.2, on line 586, modify the command line detection syntax:

<?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, now you’re done.

Run under the Linux command line. Note that you need to switch to the thinkphp root directory here

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

Check the running status:

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

The above is the detailed content of How to match workerman with tp. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools