public static function register_signal()
 {
     swoole_process::signal(SIGUSR2, function ($signal_num) {
         echo "收到结束信号,结束进程\n";
         exit;
     });
 }
Exemple #2
0
 function run($options)
 {
     $asDaemon = isset($options['asDaemon']) ? $options['asDaemon'] : 0;
     if ($asDaemon) {
         \swoole_process::daemon();
     }
     $pids = [];
     $workers = [];
     for ($i = 0; $i < $this->worker_num; $i++) {
         $process = new \swoole_process($this->workerStart, $this->redirect_stdout);
         $process->id = $i;
         $pid = $process->start();
         $pids[] = $pid;
         $workers[$pid] = $process;
     }
     $pidFile = isset($options['pidFile']) ? $options['pidFile'] : 0;
     if ($pidFile) {
         $ppid = posix_getpid();
         $pids[] = $ppid;
         file_put_contents($pidFile, implode("|", $pids));
     }
     \swoole_process::signal(SIGTERM, function () use($workers) {
         exit(0);
     });
     \swoole_process::signal(SIGINT, function () {
         exit(0);
     });
     \swoole_process::wait(false);
     return $workers;
 }
Exemple #3
0
 /**
  * 注册信号
  */
 public static function registerSignal()
 {
     //终止进程
     \swoole_process::signal(SIGTERM, function ($signo) {
         self::exit2p('master process [' . self::$process_name . '] exit');
     });
     //忽略信号,当子进程停止或退出时通知父进程
     \swoole_process::signal(SIGCHLD, function ($signo) {
         //非阻塞等待
         while ($ret = \swoole_process::wait(false)) {
             $pid = $ret['pid'];
             if (isset(self::$worker_list[$pid])) {
                 $task = self::$worker_list[$pid];
                 $id = $task['id'];
                 $task['process']->close();
                 unset(self::$worker_list[$pid]);
                 if (isset(self::$unique_task_list[$id]) && self::$unique_task_list[$id] > 0) {
                     self::$unique_task_list[$id]--;
                 }
                 defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, 'child process exit:' . $pid);
             }
         }
     });
     //终止进程,用户定义信号1
     \swoole_process::signal(SIGUSR1, function ($signo) {
         //TODO something
     });
     defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, 'register signal success');
 }
Exemple #4
0
function master_process($workers)
{
    //监听子进程,如果停止,会再拉起来
    swoole_process::signal(SIGCHLD, function ($signo) use(&$workers) {
        while (1) {
            $ret = swoole_process::wait(false);
            if ($ret) {
                $pid = $ret['pid'];
                //这里实现一个自动拉起的能力
                $child_process = $workers[$pid];
                logprint('info', "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid);
                $new_pid = $child_process->start();
                $workers[$new_pid] = $child_process;
                unset($workers[$pid]);
            } else {
                break;
            }
        }
    });
    //kill -10 结束全部程序
    swoole_process::signal(SIGUSR1, function ($signo) use(&$workers) {
        swoole_process::signal(SIGCHLD, null);
        foreach ($workers as $pid => $worker) {
            swoole_process::kill($pid);
        }
        //处理子进程,然后自己退出
        exit;
    });
}
Exemple #5
0
 /**
  * 注册信号
  */
 public static function registerSignal()
 {
     //终止进程
     \swoole_process::signal(SIGTERM, function ($signo) {
         self::exit2p('master process [' . self::$process_name . '] exit');
     });
     //忽略信号,当子进程停止或退出时通知父进程
     \swoole_process::signal(SIGCHLD, function ($signo) {
         //非阻塞等待
         while ($ret = \swoole_process::wait(false)) {
             $pid = $ret['pid'];
             if (isset(self::$worker_list[$pid])) {
                 $worker = self::$worker_list[$pid];
                 $worker['process']->close();
                 //重启该子进程
                 self::createWorkerProcess($worker['className'], $worker['number'], $worker['options']);
                 defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, 'child process restart:' . $pid);
             }
         }
     });
     //终止进程,用户定义信号1
     \swoole_process::signal(SIGUSR1, function ($signo) {
         //TODO something
     });
     defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, 'register signal success');
 }
 public function onWorkerStart(swoole_server $server, $worker_id)
 {
     swoole_process::signal(SIGCHLD, function ($sig) {
         //必须为false,非阻塞模式
         while ($ret = swoole_process::wait(false)) {
             echo "PID={$ret['pid']}\n";
         }
     });
 }
Exemple #7
0
function child_async(swoole_process $worker)
{
    //echo "Worker: start. PID=".$worker->pid."\n";
    //recv data from master
    $GLOBALS['worker'] = $worker;
    global $argv;
    $worker->name("{$argv[0]}: worker");
    swoole_process::signal(SIGTERM, function ($signal_num) use($worker) {
        echo "signal call = {$signal_num}, #{$worker->pid}\n";
    });
    swoole_event_add($worker->pipe, function ($pipe) use($worker) {
        $recv = $worker->read();
        echo "From Master: {$recv}\n";
        $worker->write("hello master\n");
    });
}
            static $index = 0;
            $index = $index + 1;
            $this->process->push($index . "Hello");
            var_dump($index);
            if ($index == 10) {
                $this->process->push("exit");
                $this->process->push("exit");
                $this->process->push("exit");
                swoole_timer_clear($timer_id);
            }
        });
    }
    public function task_run($worker)
    {
        while (true) {
            $data = $worker->pop();
            var_dump($data);
            if ($data == 'exit') {
                $worker->exit();
            }
            sleep(5);
        }
    }
}
new BaseProcess();
swoole_process::signal(SIGCHLD, function ($sig) {
    //必须为false,非阻塞模式
    while ($ret = swoole_process::wait(false)) {
        echo "PID={$ret['pid']}\n";
    }
});
Exemple #9
0
                swoole_process::kill($process->pid);
            }
        }
    });
}
//开启子进程进行异步处理
function callback_function(swoole_process $worker)
{
    $GLOBALS['worker'] = $worker;
    swoole_event_add($worker->pipe, function ($pipe) {
        $worker = $GLOBALS['worker'];
        $recv = $worker->read();
        if ($recv) {
            $data = call_user_func_array('dosomething', [json_decode($recv, true)]);
            $worker->write($data);
        } else {
            $worker->write('');
        }
    });
}
swoole_process::signal(SIGCHLD, function ($signo) use($worker_num) {
    static $worker_count = 0;
    while ($ret = swoole_process::wait(false)) {
        echo 'Worker Exit, pid=', $ret['pid'], PHP_EOL;
        $worker_count++;
        if ($worker_count >= $worker_num) {
            echo memory_get_usage(true) / 1024 / 1024, PHP_EOL;
            swoole_event_exit();
        }
    }
});
Exemple #10
0
function my_onWorkerStart(swoole_server $serv, $worker_id)
{
    processRename($serv, $worker_id);
    if (!$serv->taskworker) {
        swoole_process::signal(SIGUSR2, function ($signo) {
            echo "SIGNAL: {$signo}\n";
        });
        //        swoole_timer_tick(2000, function($id) {
        //            var_dump($id);
        //        });
    } else {
        $serv->tick(1000, function ($id) use($serv) {
            if (G::$index > 10) {
                $serv->after(2500, 'timer_show', 2);
                G::$index = 0;
            } else {
                G::$index++;
            }
            timer_show($id);
        });
    }
    //forkChildInWorker();
    //	setTimerInWorker($serv, $worker_id);
}
Exemple #11
0
{
    $binpath = $_SERVER["_"];
    $worker->exec($binpath, array(ROOT_PATH . "http_test.php", $worker->pipe));
}
$process = new swoole_process("fork");
$process->start();
function process_run($worker)
{
    echo "abc\n";
    $worker->exit(0);
    exit;
}
function create_process()
{
    $process = new swoole_process("process_run");
    $process->start();
}
swoole_timer_add(1000, function ($interval) {
    create_process();
    create_process();
});
swoole_process::signal(SIGCHLD, function ($signo) {
    while ($pid = pcntl_wait($status, WNOHANG)) {
        echo $pid . "\n";
    }
});
swoole_event_add($process->pipe, function ($pipe) use($process) {
    $ret = $process->read();
    echo $ret;
    $process->write($ret);
});
Exemple #12
0
function my_onWorkerStart(swoole_server $serv, $worker_id)
{
    processRename($serv, $worker_id);
    if (!$serv->taskworker) {
        swoole_process::signal(SIGUSR2, function ($signo) {
            echo "SIGNAL: {$signo}\n";
        });
        $serv->defer(function () {
            echo "defer call\n";
        });
    } else {
        //        swoole_timer_after(2000, function() {
        //            echo "after 2 secends.\n";
        //        });
        //        $serv->tick(1000, function ($id) use ($serv) {
        //            if (G::$index > 10) {
        //                $serv->after(2500, 'timer_show', 2);
        //                G::$index = 0;
        //            } else {
        //                G::$index++;
        //            }
        //            timer_show($id);
        //        });
    }
    //forkChildInWorker();
    //	setTimerInWorker($serv, $worker_id);
}
 /**
  * 注册信号
  */
 private static function register_signal()
 {
     swoole_process::signal(SIGTERM, function ($signo) {
         self::exit2p("收到退出信号,退出主进程");
     });
     swoole_process::signal(SIGCHLD, function ($signo) {
         while ($ret = swoole_process::wait(false)) {
             $pid = $ret['pid'];
             if (isset(self::$task_list[$pid])) {
                 $task = self::$task_list[$pid];
                 if ($task["type"] == "crontab") {
                     $end = microtime(true);
                     $start = $task["start"];
                     $id = $task["id"];
                     Main::log_write("{$id} [Runtime:" . sprintf("%0.6f", $end - $start) . "]");
                     $task["process"]->close();
                     //关闭进程
                     unset(self::$task_list[$pid]);
                     if (isset(self::$unique_list[$id]) && self::$unique_list[$id] > 0) {
                         self::$unique_list[$id]--;
                     }
                 }
                 if ($task["type"] == "worker") {
                     $end = microtime(true);
                     $start = $task["start"];
                     $classname = $task["classname"];
                     Main::log_write("{$classname}_{$task["number"]} [Runtime:" . sprintf("%0.6f", $end - $start) . "]");
                     $task["process"]->close();
                     //关闭进程
                     (new Worker())->create_process($classname, $task["number"], $task["redis"]);
                 }
             }
         }
     });
     swoole_process::signal(SIGUSR1, function ($signo) {
         //TODO something
     });
 }
Exemple #14
0
 function child_async(swoole_process $worker)
 {
     //echo "Worker: start. PID=".$worker->pid."\n";
     global $argv;
     $worker->name("{$argv[0]}: worker #" . $worker->id . "/" . $this->worker_num);
     swoole_process::signal(SIGTERM, function ($signal_num) use($worker) {
         echo "signal call = {$signal_num}, #{$worker->pid}\n";
     });
     switch ($this->status) {
         case 'import':
             echo " worker #" . $worker->id . "/" . $this->worker_num . " enter status: " . $this->status . PHP_EOL;
             $this->import_data($worker);
             break;
         case '1-dim':
             echo " worker #" . $worker->id . "/" . $this->worker_num . " enter status: " . $this->status . PHP_EOL;
             $this->generate_1dim_rank($worker);
             break;
         case '2-dim':
             echo " worker #" . $worker->id . "/" . $this->worker_num . " enter status: " . $this->status . PHP_EOL;
             $this->generate_2dim_rank($worker);
             break;
         case '3-dim':
             echo " worker #" . $worker->id . "/" . $this->worker_num . " enter status: " . $this->status . PHP_EOL;
             $this->generate_3dim_rank($worker);
             break;
         default:
             break;
     }
     $worker->exit(0);
 }
Exemple #15
0
function my_onWorkerStart($serv, $worker_id)
{
    processRename($serv, $worker_id);
    if (!$serv->taskworker) {
        swoole_process::signal(SIGUSR2, function ($signo) {
            echo "SIGNAL: {$signo}\n";
        });
    }
    //forkChildInWorker();
    //	setTimerInWorker($serv, $worker_id);
}
Exemple #16
0
/**
 * @param $signo
 * @param callable $callback
 * @return mixed
 */
function process_signal($signo, callable $callback)
{
    return swoole_process::signal($signo, $callback);
}
Exemple #17
0
 /**
  * @param int $worker_num
  * @param bool $daemon
  * @throws Exception\NotFound
  */
 function runWorker($worker_num = 1, $daemon = false)
 {
     if ($worker_num > 1 or $daemon) {
         if (!class_exists('\\swoole\\process')) {
             throw new Exception\NotFound("require swoole extension");
         }
         if ($worker_num < 0 or $worker_num > 1000) {
             $worker_num = 200;
         }
     } else {
         $this->_atomic = new \swoole_atomic(1);
         $this->_worker();
         return;
     }
     if ($daemon) {
         \swoole_process::daemon();
     }
     $this->_atomic = new \swoole_atomic(1);
     for ($i = 0; $i < $worker_num; $i++) {
         $process = new \swoole\process(array($this, '_worker'), false, false);
         $process->start();
         $this->_workers[] = $process;
     }
     \swoole_process::signal(SIGCHLD, function () {
         while (true) {
             $exitProcess = \swoole_process::wait(false);
             if ($exitProcess) {
                 foreach ($this->_workers as $k => $p) {
                     if ($p->pid == $exitProcess['pid']) {
                         if ($this->_atomic->get() == 1) {
                             $p->start();
                         } else {
                             unset($this->_workers[$k]);
                             if (count($this->_workers) == 0) {
                                 swoole_event_exit();
                             }
                         }
                     }
                 }
             } else {
                 break;
             }
         }
     });
     \swoole_process::signal(SIGTERM, function () {
         //停止运行
         $this->_atomic->set(0);
     });
 }
 /**
  * 注册信号
  */
 private static function register_signal()
 {
     //注册子进程退出信号逻辑
     swoole_process::signal(SIGCHLD, function ($signo) {
         while (($pid = pcntl_wait($status, WNOHANG)) > 0) {
             Main::log_write("收到子进程{$pid}退出信号");
             if (!isset(Squire_Master::$workers[$pid]["logout"])) {
                 $task = Squire_Master::$workers[$pid]["task"];
                 Squire_Master::create_child_process($task, Squire_Master::$task_list[$task]);
             }
             unset(Squire_Master::$workers[$pid]);
         }
     });
     //注册主进程退出逻辑
     swoole_process::signal(SIGTERM, function ($signo) {
         Main::log_write("收到主进程退出信号, 发送子进程退出信号:" . $signo);
         foreach (Squire_Master::$workers as $pid => $process) {
             Squire_Master::$workers[$pid]["logout"] = true;
             swoole_process::kill($pid, SIGUSR2);
         }
         if (!empty(Main::$http_server)) {
             swoole_process::kill(Main::$http_server->pid, SIGKILL);
         }
         Main::log_write("已发送子进程退出信号,主进程正在退出.....");
         swoole_timer_add(501, function () {
             if (count(Squire_Master::$workers) == 0) {
                 Squire_Master::exit2p("主进程退出成功");
             }
         });
     });
     //注册重新载入配置信号
     swoole_process::signal(SIGUSR1, function ($signo) {
         Main::log_write("收到重新载入配置信号:" . $signo);
         Squire_Master::reload();
     });
 }
Exemple #19
0
 public function initChildSignal()
 {
     \swoole_process::signal(SIGUSR1, array($this, 'signalChildCallback'));
 }
Exemple #20
0
     * 创建一个新的客户端
     */
    static function create()
    {
        $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
        $client->on("connect", 'G::onConnect');
        $client->on("receive", 'G::onReceive');
        $client->on("error", function (swoole_client $cli) {
            echo "error\n";
        });
        $client->on("close", 'G::onClose');
        $client->connect('127.0.0.1', 9502);
        self::$count++;
        self::putLog("CREATE#" . $client->sock . "\$");
    }
    static function broadcast()
    {
        foreach (self::$clients as $cli) {
            $cli->send(self::SEND_DATA);
        }
    }
}
for ($i = 0; $i < G::MAX_CLIENTS; $i++) {
    G::create();
}
swoole_timer_tick(1000, 'G::broadcast');
swoole_process::signal(SIGTERM, function () {
    echo "create count=" . G::$count . "\n";
    swoole_event_exit();
});
swoole_event_wait();
Exemple #21
0
{
    global $workers;
    $worker = $workers[$pipe];
    $data = $worker->read();
    echo "RECV: " . $data;
}
//循环创建进程
for ($i = 0; $i < $worker_num; $i++) {
    $process = new swoole_process(function (swoole_process $process) {
        $i = 1;
        while ($i++) {
            $process->write("Worker#{$process->id}: hello master\n");
            if ($i > 5 and $process->id == 1) {
                $process->exit();
            }
            sleep(1);
        }
    });
    $process->id = $i;
    $pid = $process->start();
    $workers[$process->pipe] = $process;
}
swoole_process::signal(SIGCHLD, function () {
    //表示子进程已关闭,回收它
    $status = swoole_process::wait();
    echo "Worker#{$status['pid']} exit\n";
});
//将子进程的管道加入EventLoop
foreach ($workers as $process) {
    swoole_event_add($process->pipe, 'onReceive');
}
Exemple #22
0
<?php

function timeout($tm)
{
    echo time() . " Timeout #{$tm}\n";
    if ($tm == 3) {
        global $timer4;
        swoole_timer_clear($timer4);
    }
}
$timer1 = swoole_timer_after(1000, function ($id) {
    echo "hello world";
    global $timer1;
    swoole_timer_clear($timer1);
}, 1);
$timer2 = swoole_timer_after(2000, 'timeout', 2);
$timer3 = swoole_timer_after(4000, 'timeout', 3);
$timer4 = swoole_timer_after(8000, 'timeout', 4);
$timer5 = swoole_timer_after(10000, 'timeout', 5);
swoole_process::signal(SIGTERM, function () {
    swoole_event_exit();
});
var_dump($timer1, $timer2, $timer3, $timer4, $timer5);
 /**
  * 注册信号
  */
 private static function register_signal()
 {
     swoole_process::signal(SIGTERM, function ($signo) {
         if (!empty(Main::$http_server)) {
             swoole_process::kill(Main::$http_server->pid, SIGKILL);
         }
         self::exit2p("收到退出信号,退出主进程");
     });
     swoole_process::signal(SIGCHLD, function ($signo) {
         while (($pid = pcntl_wait($status, WNOHANG)) > 0) {
             $task = self::$task_list[$pid];
             $end = microtime(true);
             $start = $task["start"];
             $id = $task["id"];
             Main::log_write("{$id} [Runtime:" . sprintf("%0.6f", $end - $start) . "]");
             unset(self::$task_list[$pid]);
             if (isset(self::$unique_list[$id]) && self::$unique_list[$id] > 0) {
                 self::$unique_list[$id]--;
             }
         }
     });
     swoole_process::signal(SIGUSR1, function ($signo) {
         LoadConfig::reload_config();
     });
 }
 /**
  *注册监听的信号
  */
 private static function registerSignal()
 {
     swoole_process::signal(SIGCHLD, function ($signo) {
         //SIGCHLD,子进程结束时,父进程会收到这个信号
         //这里可以做任务执行完后的事情,比如:改变任务状态,统计任务执行时间
         while ($status = swoole_process::wait(false)) {
             $task = self::$taskList[$status['pid']];
             $startTime = $task['start'];
             self::updateTasks($task);
             $runTime = time() - $startTime;
             Main::log($task['task']['name'] . "执行了" . $runTime . "秒");
             unset(self::$taskList[$status['pid']]);
         }
     });
     swoole_process::signal(SIGINT, function ($signo) {
         self::resetStatus();
         unlink(Main::$pidFile);
         exit;
     });
     swoole_process::signal(SIGUSR1, function () {
         self::init();
     });
 }
Exemple #25
0
<?php

swoole_process::signal(SIGALRM, function () {
    static $i = 0;
    echo "#{$i}\talarm\n";
    $i++;
    if ($i > 20) {
        swoole_process::alarm(-1);
    }
});
swoole_process::alarm(100 * 1000);