Ejemplo n.º 1
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);
     });
 }
Ejemplo n.º 2
0
<?php

$atomic = new swoole_atomic(123);
echo $atomic->add(12) . "\n";
echo $atomic->sub(11) . "\n";
echo $atomic->cmpset(122, 999) . "\n";
echo $atomic->cmpset(124, 999) . "\n";
echo $atomic->get() . "\n";