public static function resetStd()
{
if (!static::$daemonize || static::$_OS !== 'linux') {
return;
}
global $STDOUT, $STDERR;
$handle = fopen(static::$stdoutFile, "a");
if ($handle) {
unset($handle);
//關(guān)閉標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤
@fclose(STDOUT);
@fclose(STDERR);
//a 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建之。
//把標(biāo)準(zhǔn)輸出和錯誤 定位到 /dev/null
$STDOUT = fopen(static::$stdoutFile, "a");
$STDERR = fopen(static::$stdoutFile, "a");
} else {
throw new Exception('can not open stdoutFile ' . static::$stdoutFile);
}
}
不是太理解這段操作的意義在于什么?
意思是把標(biāo)準(zhǔn)錯誤流 (stdout) 、標(biāo)準(zhǔn)錯誤流(stderr)重定向到設(shè)備/dev/null上。
/dev/null 是類Unix系統(tǒng)中的一個(gè)特殊文件設(shè)備,他的作用是接受一切輸入它的數(shù)據(jù)并丟棄這些數(shù)據(jù)。通常被當(dāng)做垃圾桶來用。
將輸出流重定向到它上面,就是丟棄這個(gè)輸出流上的所有輸出。
還有一點(diǎn),不太理解的是,定義的onMessage 為什么會執(zhí)行2次,這是個(gè)什么原理?
操作就是: 開啟workerman服務(wù)后,刷新瀏覽器。
具體代碼:
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->count = 3;
$http_worker->onMessage = function($connection, $data)
{
echo "xx\n";
$connection->send('hello world');
};