本文整理汇总了PHP中swoole_process::wait方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_process::wait方法的具体用法?PHP swoole_process::wait怎么用?PHP swoole_process::wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_process
的用法示例。
在下文中一共展示了swoole_process::wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
});
}
示例2: 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])) {
$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');
}
示例3: 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;
}
示例4: master_async
function master_async($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];
//unset($workers[$pid]);
echo "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid . PHP_EOL;
$new_pid = $child_process->start();
$workers[$new_pid] = $child_process;
unset($workers[$pid]);
} else {
break;
}
}
});
/**
* @var $process swoole_process
*/
foreach ($workers as $pid => $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$recv = $process->read();
if ($recv) {
echo "From Worker: " . $recv;
}
$process->write("HELLO worker {$process->pid}\n");
});
$process->write("hello worker[{$pid}]\n");
}
}
示例5: master_async
function master_async($workers)
{
swoole_process::signal(SIGCHLD, function ($signo) use($workers) {
while (1) {
$ret = swoole_process::wait(false);
if ($ret) {
$pid = $ret['pid'];
unset($workers[$pid]);
echo "Worker Exit, PID=" . $pid . PHP_EOL;
} else {
break;
}
}
});
/**
* @var $process swoole_process
*/
foreach ($workers as $pid => $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$recv = $process->read();
if ($recv) {
echo "From Worker: " . $recv;
}
});
$process->write("hello worker[{$pid}]\n");
}
}
示例6: run
static function run()
{
$table = new swoole_table(1024 * 256);
$table->column('index', swoole_table::TYPE_INT);
$table->column('serid', swoole_table::TYPE_INT);
$table->column('data', swoole_table::TYPE_STRING, 64);
$table->create();
self::$table = $table;
for ($i = 0; $i < self::$key_num; $i++) {
$key = 'user_' . (self::$key_base + $i);
$ret = self::$table->set($key, array('index' => $i, 'serid' => rand(1000, 9999), 'data' => "hello_world_{$i}"));
if (!$ret) {
echo "count {$i} failed.";
break;
}
}
for ($i = 0; $i < self::$worker_num; $i++) {
$process = new swoole_process('UnitTest_Table::worker');
$process->start();
$workers[$i] = $process;
}
for ($i = 0; $i < self::$worker_num; $i++) {
$exit = swoole_process::wait();
echo "worker[{$i}] exit\n";
}
}
示例7: 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";
}
});
}
示例8: master_run
private function master_run()
{
$event = event_new();
event_set($event, $this->socket_fd, EV_READ | EV_PERSIST, array($this, 'dispatch'));
event_base_set($event, $this->event_base);
event_add($event);
event_base_loop($this->event_base);
swoole_process::wait();
}
示例9: master_async
function master_async()
{
$count = 0;
swoole_process::signal(SIGCHLD, function ($signo) use(&$count) {
while (1) {
$ret = swoole_process::wait(false);
if ($ret) {
$pid = $ret['pid'];
$count++;
//echo "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid . PHP_EOL;
//echo "Count=" . $count . " Worker_num=" . $this->worker_num . " Stauts=" . $this->status . PHP_EOL;
if ($count == $this->worker_num) {
switch ($this->status) {
case '3-dim':
$this->report_cost();
exit(0);
break;
case 'import':
$this->report_cost();
$this->status = '1-dim';
$this->restart_process();
break;
case '1-dim':
$this->report_cost();
$this->status = '2-dim';
$this->restart_process();
break;
case '2-dim':
$this->report_cost();
$this->status = '3-dim';
$this->restart_process();
break;
default:
break;
}
$count = 0;
}
} else {
break;
}
}
});
/**
* @var $process swoole_process
*/
foreach ($this->workers as $pid => $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$recv = $process->read();
if ($recv) {
echo "From Worker: " . $recv;
}
$process->write("HELLO worker {$process->pid}\n");
});
$process->write("hello worker[{$pid}]\n");
}
}
示例10: callback_timer_function
function callback_timer_function()
{
$Handle = new TimingCall_Tasks();
$Handle->upDaemonTime();
$tasks = $Handle->readExecuteTasks();
foreach ($tasks as $task) {
$process = new swoole_process('callback_process_function', true);
$process->write(json_encode($task));
$process->start();
$process->wait(false);
}
}
示例11: onReceive
function onReceive($pipe)
{
global $workers;
$worker = $workers[$pipe];
$data = $worker->read();
if ($data == false) {
//表示子进程已关闭,回收它
$status = swoole_process::wait();
echo "Worker#{$status['pid']} exit\n";
} else {
echo "RECV: " . $data;
}
}
示例12: start
/**
* @brief 启动子进程
*/
public function start()
{
$workerNum = $this->_objAha->getConfig()->get('aha', 'worker_num');
for ($i = 0; $i < $workerNum; $i++) {
$worker = new \Daemon\Asyncworker($this->_objAha);
$process = new \swoole_process(array($worker, 'start'));
//$process->daemon();
$workerPid = $process->start();
$this->_workers[$workerPid] = $process;
$process->write("worker started!");
}
foreach ($this->_workers as $process) {
$workerPid = \swoole_process::wait();
echo "[Worker Shutdown][WorkerId] {$workerPid} " . PHP_EOL;
unset($this->_workers[$workerPid]);
}
}
示例13: _waitChildProcess
protected function _waitChildProcess()
{
//必须为false,非阻塞模式
//信号发生时可能同时有多个子进程退出
//必须循环执行wait直到返回false
$objManager = \Daemon\Library\Ipc\Manager::getInstance();
while ($result = \swoole_process::wait(false)) {
$pid = $result['pid'];
if ($objManager->hasDriveWorker($pid)) {
$objManager->delDriveWorker($pid);
\Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid);
if (!$this->_sigterm) {
$objManager->createDriveWorker(\Daemon\Process\Master::getInstance()->getAha(), 1);
}
} elseif ($objManager->hasTaskWorker($pid)) {
$objManager->delTaskWorker($pid);
\Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid);
if (!$this->_sigterm) {
$objManager->createDirtributeWorker(\Daemon\Process\Master::getInstance()->getAha(), 1);
}
} elseif ($objManager->hasRedoWorker($pid)) {
$objManager->delRedoWorker($pid);
\Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid);
if (!$this->_sigterm) {
$objManager->createRedoWorker(\Daemon\Process\Master::getInstance()->getAha(), 1);
}
} elseif ($objManager->hasStatsWorker($pid)) {
$objManager->delStatsWorker($pid);
\Daemon\Library\Ipc\Shared::delCurrentTaskTable($pid);
if (!$this->_sigterm) {
$objManager->createStatsWorker(\Daemon\Process\Master::getInstance()->getAha(), 1);
}
} else {
}
}
//如果是SIGTERM信号 并且子进程都已经退出了 父进程终止
$workers = \Daemon\Library\Ipc\Shared::getCurrentTaskTable();
if ($this->_sigterm && !$workers->count()) {
\swoole_event_exit();
}
}
示例14: 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";
}
});
示例15: StartServ
function StartServ($phpStart, $cmd, $name)
{
$process = new swoole_process(function (swoole_process $worker) use($name, $cmd, $phpStart) {
//目前指支持一个
$worker->exec($phpStart, array(STARTBASEPATH . "/lib/Swoole/shell/start.php", $cmd, $name));
//拉起server
StartLogTimer(__LINE__ . ' ' . $phpStart . ' ' . STARTBASEPATH . '/lib/Swoole/shell/start.php ' . $cmd . ' ' . $name);
}, false);
$pid = $process->start();
$exeRet = swoole_process::wait();
if ($exeRet['code']) {
//创建失败
StartLog(" startall [31;40m [FAIL] [0m" . PHP_EOL);
return false;
} else {
StartLog(" startall [31;40m [SUCCESS] [0m" . PHP_EOL);
return true;
}
}