• Swoole实战之手撸HttpServer框架 14 支持命令参数自动和停止我们的服务、给框架起名 与 php内置变量 $argc $argv


    视频地址 https://www.bilibili.com/video/BV14E411t7T4?p=22&spm_id_from=pageDriver&vd_source=4a69745b599dffec877b0fcfe130b092

    僵尸进程

    \pro\test2.php

    <?php
    $http = new Swoole\Http\Server('0.0.0.0',80);
    //配置参数 https://wiki.swoole.com/#/server/setting
    $http->set(array(
        'worker_num'=>1,     //设置启动的 Worker 进程数。【默认值:CPU 核数】
        'daemonize'=>false     // 使用docker 不需要设置守护进程
    ));
    $http->on('request',function ($req,$res){
    
    });
    $http->start();

    启动后会有3个进程

    1个master经常(服务器主进程)

    1个Manager进程

    work _num个work进程

    php内置变量 $argc $argv

    比如 php test2.php start

    则 $argc=2   、$argv=['test2.php','start']

    <?php
    use Swoole\Http\Server;
    use Swoole\Process;
    
    if ($argc==2){
        $cmd = $argv[1];
        if ($cmd=='start'){
            $http = new Swoole\Http\Server('0.0.0.0',80);
            //配置参数 https://wiki.swoole.com/#/server/setting
            $http->set(array(
              'worker_num'=>1,     //设置启动的 Worker 进程数。【默认值:CPU 核数】
              'daemonize'=>false     // 使用docker 不需要设置守护进程
            ));
            $http->on('request',function ($req,$res){
            
            });
        
            $http->on('Start',function (Server $server ){
                $mid =  $server->master_pid;   //返回当前服务器主进程的 PID。
                file_put_contents("./ttt.pid",$mid);  //会覆盖
            });
            $http->start();
        
        } elseif ($cmd=='stop'){
            $mid = intval(file_get_contents("./ttt.pid"));
            if (trim($mid)){
                Process::kill($mid);
            }
        } else {
            echo '无效命令';
        }
    }   else {
        echo '无效命令';
    }

    重启容器 

    docker exec -it 67e09c446436 sh

    重新开一个窗口

     stop以后会出现     NOTICE    Server is shutdown now.

  • 相关阅读:
    elastalert邮件告警
    Kubernetes(k8s)集群安装
    supervisord进程管理
    Flask Ansible自动化平台搭建(持续更新)
    pandas数据导出Execl
    docker运行dubbo-admin
    Activemq集群搭建
    Zabbix自动发现java进程
    selenium爬取百度图片
    Beta 冲刺(1/7)
  • 原文地址:https://www.cnblogs.com/polax/p/16441509.html
Copyright © 2020-2023  润新知