本文整理汇总了PHP中pcntl_wait函数的典型用法代码示例。如果您正苦于以下问题:PHP pcntl_wait函数的具体用法?PHP pcntl_wait怎么用?PHP pcntl_wait使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pcntl_wait函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: cleanChildren
/** @return bool did a child exit? */
private function cleanChildren()
{
$status = null;
if ($exited = pcntl_wait($status, WNOHANG)) {
unset($this->workerProcesses[$exited]);
$this->getLogger()->info("Worker {$exited} got WNOHANG during normal operation");
return true;
}
return false;
}
示例2: wait
function wait()
{
global $fatherpid;
if (posix_getpid() == $fatherpid) {
tlog("begin wait");
while (($pid = pcntl_wait($stat)) > 0) {
tlog("child." . $pid . ". terminated \n");
}
}
}
示例3: run
public function run()
{
$this->numberOfThreadsToLaunch = $this->numberOfTasks;
if ($this->maxThreads == 0 || count($this->pool) <= $this->maxThreads) {
$this->launchAllTasks();
} else {
while ($this->numberOfRunningThreads < $this->maxThreads) {
$this->launchNextTask();
}
}
while (1) {
// echo "waiting\n";
$pid = pcntl_wait($extra);
if ($pid == -1) {
break;
}
// echo ": task done : $pid\n";
$this->finishTask($pid);
if ($this->numberOfThreadsToLaunch) {
$this->launchNextTask();
}
}
// echo "processes done ; exiting\n";
return;
}
示例4: execute
/**
* Create a daemon process.
*
* @return DaemonHttpApplication Return self to support chaining.
*/
public function execute()
{
// Create first child.
if (pcntl_fork()) {
// I'm the parent
// Protect against Zombie children
pcntl_wait($status);
exit;
}
// Make first child as session leader.
posix_setsid();
// Create second child.
$pid = pcntl_fork();
if ($pid) {
// If pid not 0, means this process is parent, close it.
$this->processId = $pid;
$this->storeProcessId();
exit;
}
$this->addLog('Daemonized');
fwrite(STDOUT, "Daemon Start\n-----------------------------------------\n");
$this->registerSignalHandler();
// Declare ticks to start signal monitoring. When you declare ticks, PCNTL will monitor
// incoming signals after each tick and call the relevant signal handler automatically.
declare (ticks=1);
while (true) {
$this->doExecute();
sleep(1);
}
return $this;
}
示例5: dealMission
/**
* 多进程处理任务
* @param callback $mission_func 子进程要进行的任务函数
*/
public function dealMission($mission_func)
{
$this->_mission_func = $mission_func;
for ($i = 0; $i < $this->_process_num; $i++) {
$pid[] = pcntl_fork();
if ($pid[$i] == 0) {
//等于0时,是子进程
$this->_func_obj->{$mission_func}($i + 1);
//结束当前子进程,以防止生成僵尸进程
if (function_exists("posix_kill")) {
posix_kill(getmypid(), SIGTERM);
} else {
system('kill -9' . getmypid());
}
exit;
} else {
if ($pid > 0) {
//大于0时,是父进程,并且pid是产生的子进程的PID
//TODO 可以记录下子进程的pid,用pcntl_wait_pid()去等待程序并结束
pcntl_wait($status);
} else {
throw new Exception('fork fail');
}
}
}
}
示例6: __construct
public function __construct(callable $callBack = null)
{
$plug = \PMVC\plug(PLUGIN);
$plug->log("Monitor starting.");
while (empty($plug[IS_STOP_ALL]) || count($plug[CHILDREN])) {
if (!empty($plug[MY_PARENT])) {
break;
}
// Check for exited children
$pid = pcntl_wait($status, WNOHANG);
if (isset($plug[CHILDREN][$pid])) {
$exitCode = pcntl_wexitstatus($status);
$plug->log("Child {$pid} was stopped with exit code of {$exitCode}");
if (!$plug[IS_STOP_ALL] && 1 !== $exitCode) {
$callbackId = $plug[CHILDREN][$pid];
$plug['start']->restore($callbackId);
}
$plug->cleanPid($pid);
}
pcntl_signal_dispatch();
if (empty($plug[CHILDREN])) {
$plug[IS_STOP_ALL] = true;
break;
}
if ($callBack && empty($plug[IS_STOP_ALL])) {
call_user_func($callBack);
}
// php will eat up your cpu if you don't have this
usleep(50000);
pcntl_signal_dispatch();
}
$plug->log("Monitor was exited.");
}
示例7: start
/**
* Start the task manager
*
* @param AbstractTask $task Task to start
*
* @return void
*/
public function start(AbstractTask $task)
{
$pid = pcntl_fork();
if ($pid == -1) {
throw new \Exception('[Pid:' . getmypid() . '] Could not fork process');
} elseif ($pid) {
$this->_activeThreads[$pid] = true;
// Reached maximum number of threads allowed
if ($this->maxThreads == count($this->_activeThreads)) {
// Parent Process : Checking all children have ended (to avoid zombie / defunct threads)
while (!empty($this->_activeThreads)) {
$endedPid = pcntl_wait($status);
if (-1 == $endedPid) {
$this->_activeThreads = array();
}
unset($this->_activeThreads[$endedPid]);
}
}
} else {
$task->initialize();
// On success
if ($task->process()) {
$task->onSuccess();
} else {
$task->onFailure();
}
posix_kill(getmypid(), 9);
}
pcntl_wait($status, WNOHANG);
}
示例8: run
public function run()
{
$count = 0;
$socket = stream_socket_server($this->socketString, &$errno, &$errstr);
if (!$socket) {
throw new RuntimeException($errstr);
}
while (true) {
while (count($this->children) < $this->maxConnections) {
$count++;
$pid = pcntl_fork();
if ($pid == -1) {
throw new RuntimeException('Couldn\'t fork');
} elseif ($pid == 0) {
$this->createChild($socket, $count);
exit;
}
$this->children[] = $pid;
}
while (pcntl_wait($status, WNOHANG or WUNTRACED) > 0) {
usleep(500000);
}
while (list($key, $val) = each($this->children)) {
if (!posix_kill($val, 0)) {
unset($this->children[$key]);
}
}
$this->children = array_values($this->children);
usleep(500000);
}
}
示例9: _doTestStartAll
protected function _doTestStartAll($baseConfig, $useFactoryMethodToCreateKeeper = false)
{
$config = $baseConfig;
$config['quantity'] = $quantity = 3;
if (!$useFactoryMethodToCreateKeeper) {
$keeper = new OneForOneKeeper(array(new Config($config)));
} else {
$keeper = \Comos\Qpm\Supervision\Supervisor::oneForOne($config)->getKeeper();
}
$keeper->startAll();
$pids = array();
for ($i = 0; $i < $quantity; $i++) {
$status = 0;
$pids[\pcntl_wait($status)] = true;
}
$currentPid = \posix_getpid();
$r = \file_get_contents($this->_logFile);
$lines = explode("\n", $r);
$count = 0;
foreach ($lines as $line) {
if (trim($line)) {
list($pid, $ppid) = explode(',', $line);
$count++;
$this->assertTrue($pids[$pid]);
$this->assertEquals($currentPid, $ppid);
}
}
$this->assertEquals($quantity, $count);
}
示例10: run
/**
* Run jobs
*
* Run all jobs provided by the job provider.
*
* Jobs are run parallel in the background. The number of jobs executed in
* parallel can be specified as the second parameter.
*
* Returns once all jobs have been executed.
*
* @param JobProvider $jobs
* @param int $parallel
* @return void
*/
public function run(JobProvider $jobs, $parallel = 4)
{
$this->logger->startExecutor($this, $jobs);
$forks = array();
$jobNr = 0;
while ($jobs->hasJobs() || count($forks)) {
while (count($forks) < $parallel && ($job = $jobs->getNextJob())) {
$this->logger->progressJob(++$jobNr);
if (($forks[] = pcntl_fork()) === 0) {
// We are the newly forked child, just execute the job
call_user_func($job);
exit(0);
}
}
do {
// Check if the registered jobs are still alive
if ($pid = pcntl_wait($status)) {
// Job has finished
$jobId = array_search($pid, $forks);
unset($forks[$jobId]);
}
} while (count($forks) >= $parallel);
}
$this->logger->finishedExecutor();
}
示例11: HandleSignals
/**
* Signals handler function
*
* @param integer $signal
* @final
*/
public final function HandleSignals($signal)
{
$this->Logger->debug("HandleSignals received signal {$signal}");
if ($signal == SIGUSR2) {
$this->Logger->debug("Recived SIGUSR2 from one of childs");
$this->ProcessManager->PIDs = array();
$this->ProcessManager->ForkThreads();
return;
}
$pid = @pcntl_wait($status, WNOHANG | WUNTRACED);
if ($pid > 0) {
$this->Logger->debug("Application received signal {$signal} from child with PID# {$pid} (Exit code: {$status})");
foreach ((array) $this->ProcessManager->PIDs as $ipid => $ipid_info) {
if ($ipid == $pid) {
unset($this->ProcessManager->PIDs[$ipid]);
if ($this->ProcessManager->PIDDir) {
$this->Logger->debug("Delete thread PID file {$ipid}");
@unlink($this->ProcessManager->PIDDir . "/" . $ipid);
}
$known_child = true;
break;
}
}
if ($known_child) {
$this->ProcessManager->ForkThreads();
} else {
$this->Logger->debug("Signal received from unknown child.");
}
}
}
示例12: start
public function start($daemon = false)
{
/*{{{*/
if ($daemon) {
if (!$this->daemonize()) {
return false;
}
}
$this->master_process_id = posix_getpid();
$this->startServer();
$this->startAgent();
while (true) {
$pid = pcntl_wait($status, WNOHANG);
switch ($pid) {
case $this->listen_process_id:
CronLogger::err("Cron server not running[{$status}], starting ...");
$this->startServer();
CronLogger::err("Cron server restart done.");
break;
case $this->agent_process_id:
CronLogger::err("Cron agent not running[{$status}], starting ...");
$this->startAgent();
CronLogger::err("Cron server restart done.");
break;
case -1:
posix_kill($this->listen_process_id, 9);
posix_kill($this->agent_process_id, 9);
$this->startAgent();
$this->startServer();
break;
}
sleep(3);
}
}
示例13: go
public function go()
{
foreach ($this->servers as $server) {
$handler = new Handler\Daemonic($server['socket'], $server['protocol'], $server['transport']);
// drop privileges after opening sockets, but before entering the working loop
// this will only work when the process is started by root
$this->dropPrivileges($server);
for ($i = 0; $i < $server['min-children']; $i++) {
$this->startWorker($handler, $server['app']);
}
}
while (true) {
pcntl_signal(SIGTERM, array($this, 'sigterm'), false);
pcntl_signal(SIGINT, array($this, 'sigterm'), false);
pcntl_signal(SIGHUP, array($this, 'sighup'), false);
$status = null;
declare (ticks=1) {
$old_pid = pcntl_wait($status);
}
if (0 === $old_pid) {
echo "[no workers]\n";
return;
}
if (-1 === $old_pid) {
continue;
// signal arrived
}
echo "[Restarting Worker]\n";
pcntl_signal(SIGTERM, SIG_DFL);
pcntl_signal(SIGINT, SIG_DFL);
pcntl_signal(SIGHUP, SIG_DFL);
$this->restartWorker($old_pid);
}
}
示例14: HandleSignals
/**
* Signals handler function
*
* @param integer $signal
* @final
*/
final public function HandleSignals($signal)
{
//Log::Log("HandleSignals received signal {$signal}", E_NOTICE);
$pid = @pcntl_wait($status, WNOHANG | WUNTRACED);
if ($pid > 0)
{
//Log::Log("Application received signal {$signal} from child with PID# {$pid} (Exit code: {$status})", E_NOTICE);
foreach((array)$this->ProcessManager->PIDs as $kk=>$vv)
{
if ($vv == $pid)
{
unset($this->ProcessManager->PIDs[$kk]);
$known_child = true;
break;
}
}
if ($known_child)
$this->ProcessManager->ForkThreads();
else
{
//Log::Log("Signal received from unknown child.", E_NOTICE);
}
}
}
示例15: start
/**
* Start the child processes.
*
* This should only be called from the command line. It should be called
* as early as possible during execution.
*
* This will return 'child' in the child processes. In the parent process,
* it will run until all the child processes exit or a TERM signal is
* received. It will then return 'done'.
*/
public function start()
{
// Trap SIGTERM
pcntl_signal(SIGTERM, array($this, 'handleTermSignal'), false);
do {
// Start child processes
if ($this->procsToStart) {
if ($this->forkWorkers($this->procsToStart) == 'child') {
return 'child';
}
$this->procsToStart = 0;
}
// Check child status
$status = false;
$deadPid = pcntl_wait($status);
if ($deadPid > 0) {
// Respond to child process termination
unset($this->children[$deadPid]);
if ($this->flags & self::RESTART_ON_ERROR) {
if (pcntl_wifsignaled($status)) {
// Restart if the signal was abnormal termination
// Don't restart if it was deliberately killed
$signal = pcntl_wtermsig($status);
if (in_array($signal, self::$restartableSignals)) {
echo "Worker exited with signal {$signal}, restarting\n";
$this->procsToStart++;
}
} elseif (pcntl_wifexited($status)) {
// Restart on non-zero exit status
$exitStatus = pcntl_wexitstatus($status);
if ($exitStatus > 0) {
echo "Worker exited with status {$exitStatus}, restarting\n";
$this->procsToStart++;
}
}
}
// Throttle restarts
if ($this->procsToStart) {
usleep(500000);
}
}
// Run signal handlers
if (function_exists('pcntl_signal_dispatch')) {
pcntl_signal_dispatch();
} else {
declare (ticks=1) {
$status = $status;
}
}
// Respond to TERM signal
if ($this->termReceived) {
foreach ($this->children as $childPid => $unused) {
posix_kill($childPid, SIGTERM);
}
$this->termReceived = false;
}
} while (count($this->children));
pcntl_signal(SIGTERM, SIG_DFL);
return 'done';
}