本文整理匯總了PHP中Thread::getCurrentThreadId方法的典型用法代碼示例。如果您正苦於以下問題:PHP Thread::getCurrentThreadId方法的具體用法?PHP Thread::getCurrentThreadId怎麽用?PHP Thread::getCurrentThreadId使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Thread
的用法示例。
在下文中一共展示了Thread::getCurrentThreadId方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: handle
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);
});
}
示例2: errorHandler
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;
}
示例3: logEmergency
public function logEmergency($str)
{
$this->logger->emergency("[CustomSocket Thread #" . \Thread::getCurrentThreadId() . "] {$str}");
}
示例4: testThreadIds
public function testThreadIds()
{
$thread = new Thread();
$this->assertInternalType("int", $thread->getThreadId());
$this->assertInternalType("int", Thread::getCurrentThreadId());
}
示例5: start
/**
* 進程啟動
*/
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);
}
示例6: blockAddress
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;
}
}
示例7: actionAsyncCallTest
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/>';
}