本文整理汇总了PHP中swoole_process::signal方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_process::signal方法的具体用法?PHP swoole_process::signal怎么用?PHP swoole_process::signal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_process
的用法示例。
在下文中一共展示了swoole_process::signal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register_signal
public static function register_signal()
{
swoole_process::signal(SIGUSR2, function ($signal_num) {
echo "收到结束信号,结束进程\n";
exit;
});
}
示例2: run
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;
}
示例3: registerSignal
/**
* 注册信号
*/
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');
}
示例4: master_process
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;
});
}
示例5: onWorkerStart
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";
}
});
}
示例6: child_async
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");
});
}
示例7: var_dump
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";
}
});
示例8: callback_function
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();
}
}
});
示例9: my_onWorkerStart
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);
}
示例10: array
{
$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);
});
示例11: my_onWorkerStart
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);
}
示例12: register_signal
/**
* 注册信号
*/
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
});
}
示例13: child_async
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);
}
示例14: my_onWorkerStart
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);
}
示例15: process_signal
/**
* @param $signo
* @param callable $callback
* @return mixed
*/
function process_signal($signo, callable $callback)
{
return swoole_process::signal($signo, $callback);
}