getCurrentThreadId() public static method

Will return the identity of the currently executing thread
public static getCurrentThreadId ( ) : integer
return integer
Beispiel #1
0
 public function handle(\Thread $thread)
 {
     $thread->start();
     echo "Start thread with ID {$thread->getCurrentThreadId()}\n";
     return Observable::create(function (ObserverInterface $observer) use($thread) {
         while ($thread->isRunning()) {
             $this->loop->tick();
         }
         try {
             echo "Thread finished\n";
             $thread->join();
             $observer->onNext(new Event('/thread/ok', $thread));
             $observer->onCompleted();
         } catch (\Exception $e) {
             echo "Thread error\n";
             $observer->onError($e);
         }
         unset($thread);
     });
 }
 public function errorHandler($errno, $errstr, $errfile, $errline, $context, $trace = null)
 {
     if (error_reporting() === 0) {
         return false;
     }
     $errorConversion = [E_ERROR => "E_ERROR", E_WARNING => "E_WARNING", E_PARSE => "E_PARSE", E_NOTICE => "E_NOTICE", E_CORE_ERROR => "E_CORE_ERROR", E_CORE_WARNING => "E_CORE_WARNING", E_COMPILE_ERROR => "E_COMPILE_ERROR", E_COMPILE_WARNING => "E_COMPILE_WARNING", E_USER_ERROR => "E_USER_ERROR", E_USER_WARNING => "E_USER_WARNING", E_USER_NOTICE => "E_USER_NOTICE", E_STRICT => "E_STRICT", E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR", E_DEPRECATED => "E_DEPRECATED", E_USER_DEPRECATED => "E_USER_DEPRECATED"];
     $errno = isset($errorConversion[$errno]) ? $errorConversion[$errno] : $errno;
     if (($pos = strpos($errstr, "\n")) !== false) {
         $errstr = substr($errstr, 0, $pos);
     }
     $oldFile = $errfile;
     $errfile = $this->cleanPath($errfile);
     $this->getLogger()->debug("[RakLib Thread #" . \Thread::getCurrentThreadId() . "] An {$errno} error happened: \"{$errstr}\" in \"{$errfile}\" at line {$errline}");
     foreach ($trace = $this->getTrace($trace === null ? 3 : 0, $trace) as $i => $line) {
         $this->getLogger()->debug($line);
     }
     return true;
 }
 public function logEmergency($str)
 {
     $this->logger->emergency("[CustomSocket Thread #" . \Thread::getCurrentThreadId() . "] {$str}");
 }
 public function testThreadIds()
 {
     $thread = new Thread();
     $this->assertInternalType("int", $thread->getThreadId());
     $this->assertInternalType("int", Thread::getCurrentThreadId());
 }
Beispiel #5
0
 /**
  * 进程启动
  */
 public function start()
 {
     // 安装信号处理函数
     $this->installSignal();
     // 添加accept事件
     $ret = $this->event->add($this->mainSocket, Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
     // 创建内部通信套接字,用于与BusinessWorker通讯
     $start_port = Man\Core\Lib\Config::get($this->workerName . '.lan_port_start');
     // 计算本进程监听的ip端口
     if (strpos(\Man\Core\Master::VERSION, 'mt')) {
         $this->lanPort = $start_port + \Thread::getCurrentThreadId() % 100;
     } else {
         $this->lanPort = $start_port - posix_getppid() + posix_getpid();
     }
     // 如果端口不在合法范围
     if ($this->lanPort < 0 || $this->lanPort >= 65535) {
         $this->lanPort = rand($start_port, 65535);
     }
     // 如果
     $this->lanIp = Man\Core\Lib\Config::get($this->workerName . '.lan_ip');
     if (!$this->lanIp) {
         $this->notice($this->workerName . '.lan_ip not set');
         $this->lanIp = '127.0.0.1';
     }
     $error_no_udp = $error_no_tcp = 0;
     $error_msg_udp = $error_msg_tcp = '';
     // 执行监听
     $this->innerMainSocketUdp = stream_socket_server("udp://" . $this->lanIp . ':' . $this->lanPort, $error_no_udp, $error_msg_udp, STREAM_SERVER_BIND);
     $this->innerMainSocketTcp = stream_socket_server("tcp://" . $this->lanIp . ':' . $this->lanPort, $error_no_tcp, $error_msg_tcp, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
     // 出错,退出,下次会换个端口
     if (!$this->innerMainSocketUdp || !$this->innerMainSocketTcp) {
         $this->notice('create innerMainSocket udp or tcp fail and exit ' . $error_msg_udp . $error_msg_tcp);
         $this->stop();
     } else {
         stream_set_blocking($this->innerMainSocketUdp, 0);
         stream_set_blocking($this->innerMainSocketTcp, 0);
     }
     // 注册套接字
     if (!$this->registerAddress($this->lanIp . ':' . $this->lanPort)) {
         $this->notice('registerAddress fail and exit');
         $this->stop();
     }
     // 添加读udp/tcp事件
     $this->event->add($this->innerMainSocketUdp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'recvInnerUdp'));
     $this->event->add($this->innerMainSocketTcp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'acceptInnerTcp'));
     // 初始化心跳包时间间隔
     $ping_interval = \Man\Core\Lib\Config::get($this->workerName . '.ping_interval');
     if ((int) $ping_interval > 0) {
         $this->pingInterval = (int) $ping_interval;
     }
     // 获取心跳包数据
     $ping_data_or_path = \Man\Core\Lib\Config::get($this->workerName . '.ping_data');
     if (is_file($ping_data_or_path)) {
         $this->pingData = file_get_contents($ping_data_or_path);
     } else {
         $this->pingData = $ping_data_or_path;
     }
     // 不返回心跳回应(客户端发来的任何数据都算是回应)的限定值
     $this->pingNotResponseLimit = (int) \Man\Core\Lib\Config::get($this->workerName . '.ping_not_response_limit');
     // 设置定时任务,发送心跳
     if ($this->pingInterval > 0) {
         \Man\Core\Lib\Task::init($this->event);
         \Man\Core\Lib\Task::add($this->pingInterval, array($this, 'ping'));
     }
     // 主体循环,整个子进程会阻塞在这个函数上
     $ret = $this->event->loop();
     // 下面正常不会执行到
     $this->notice('worker loop exit');
     // 执行到就退出
     exit(0);
 }
Beispiel #6
0
 public function blockAddress($address, $timeout = 300)
 {
     $final = microtime(true) + $timeout;
     if (!isset($this->block[$address]) or $timeout === -1) {
         if ($timeout === -1) {
             $final = PHP_INT_MAX;
         } else {
             $this->getLogger()->notice("[RakLib Thread #" . \Thread::getCurrentThreadId() . "] Blocked {$address} for {$timeout} seconds");
         }
         $this->block[$address] = $final;
     } elseif ($this->block[$address] < $final) {
         $this->block[$address] = $final;
     }
 }
 public function actionAsyncCallTest()
 {
     $runner = new AsyncRunner(function ($interval, $times, $name) {
         //dump(Thread::getCurrentThread());
         for ($i = 0; $i < $times; $i++) {
             echo "{$name} : {$i} / {$times} ....";
             Yii::log("{$name} : {$i} / {$times} ...." . Thread::getCurrentThreadId(), __FUNCTION__);
             usleep($interval);
         }
         Yii::log($name . 'done.');
         return $name . ' done.';
     }, array(100000, 10, "Jim"));
     $runner->start();
     echo $runner->getResult() . '<br/>';
 }