Timers 計時器

2022-05-16 09:08 更新

setTimeout(callback, delay, [arg], [...])

設置延時delay 毫秒之后執(zhí)行回調函數(callback)。該函數返回timeoutId,可以使用clearTimeout()清除定時。你 也可以傳遞額外的參數給回調函數。

clearTimeout(timeoutId)

清除指定的定時。

setInterval(callback, delay, [arg], [...])

設置重復延時調用callback。該函數返回intervalId,可以使用clearTimeout()清除定時。你也可以傳遞可選的 參數給回調函數。

.end(); clearInterval(intervalId)

清除指定的重復延時回調。

Child Processes 子進程

node 通過ChildProcess 類提供全面的popen(3)功能。 程序可以通過子進程的標準輸入(stdin)、標準輸出(stdout)、標準錯誤輸出(stderr)以完全非阻塞的形式傳遞數據。

你可以使用require('child_process').spawn()創(chuàng)建子進程。 每個子進程總是帶有三個流對象:child.stdin, child.stdout 和child.stderr。 每個ChildProcess 類也是一個事件觸發(fā)器。

Event: 'exit'

function (code, signal) {}

此事件在子進程結束后被觸發(fā)。如果進程正常結束,code 參數的值就是子進程退出后的返回值,否則此參數為 null。如果進程因為信號而終止,參數signal 就是信號的名稱,否則此參數為null。 觸發(fā)此事件之后,node 將不再觸發(fā)'output'和'error'事件。 See waitpid(2).

child.stdin

可寫流(Writable Stream),代表子進程的標準輸入(stdin)。使用end()函數關閉此流(stream),通常會終止子進程。

child.stdout

只讀流(Readable Stream),代表子進程的標準輸出(stdout)。 child.stderr

只讀流(Readable Stream),代表子進程的標準錯誤輸出(stderr)。

child.pid

子進程的PID Example:

var spawn = require('child_process').spawn,
grep = spawn('grep', ['ssh']);
console.log('Spawned child pid: ' + grep.pid);
grep.stdin.end();

child_process.spawn(command, args=[], [options])

使用指定的命令行參數創(chuàng)建新線程。如果不指定參數,args 默認為空數組。 第三個參數用于指定一些額外的選項,默認值如下:

{ cwd: undefined
, env: process.env,
, customFds: [-1, -1, -1]
}

cwd 可以用于指定新進程的工作目錄,env 指定新進程可見的環(huán)境變量,而customFds 則可以將新進程的 [stdin,stdout,stderr]綁定到指定的流,-1指創(chuàng)建新的流。

例子:執(zhí)行命令'ls -lh /usr'并捕獲標準輸出、標準錯誤輸出和退出代碼(程序退出時,main 函數返回的代碼)。

var sys = require('sys'),
spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
sys.print('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
sys.print('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});

例子: 實現(xiàn)'ps ax | grep ssh'命令。

var sys = require('sys'),
spawn = require('child_process').spawn,
ps = spawn('ps', ['ax']),
grep = spawn('grep', ['ssh']);
ps.stdout.on('data', function (data) {
grep.stdin.write(data);
});
ps.stderr.on('data', function (data) {
sys.print('ps stderr: ' + data);
});
ps.on('exit', function (code) {
if (code !== 0) {
console.log('ps process exited with code ' + code);
}
grep.stdin.end();
});
grep.stdout.on('data', function (data) {
sys.print(data);
});
grep.stderr.on('data', function (data) {
sys.print('grep stderr: ' + data);
});
grep.on('exit', function (code) {
if (code !== 0) {
console.log('grep process exited with code ' + code);
}
});

例子,檢測退出代碼:

var spawn = require('child_process').spawn,
child = spawn('bad_command');
child.stderr.on('data', function (data) {
if (/^execvp\(\)/.test(data.asciiSlice(0,data.length))) {
console.log('Failed to start child process.');
}
});

請參見:child_process.exec()

child_process.exec(command, [options], callback)

使用子進程執(zhí)行命令,緩存子進程的輸出,并將子進程的輸出以回調函數參數的形式返回。

var sys = require('sys'),
exec = require('child_process').exec,
child;
child = exec('cat *.js bad_file | wc -l',
function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});

回調函數得到的參數分別為(error, stdout, stderr)。成功時error 參數是null;失敗時error 參數是Error 的實例。 error.code 是子進程的返回值,error.signal 保存終止進程的信號。 exec 函數的第二個可選參數可以用來指定一些選項。默認值為

{ encoding: 'utf8'
, timeout: 0
, maxBuffer: 200*1024
, killSignal: 'SIGKILL'
, cwd: null
, env: null
}

如果timeout 為大于0的值,node 將終止運行時間超過timeout(單位為毫秒)的子進程。子進程將被終止信號 (killSignal,默認值為'SIGKILL')終止。maxBuffer 指定stdout 和stderr 最大可緩存數據的大小,如果超過這個值 子進程將被終止。

child.kill(signal='SIGTERM')

向子進程發(fā)送信號。如果不指定參數,默認發(fā)送'SIGTERM'信號。所有可用信號列表請參考signal(7)。

var spawn = require('child_process').spawn,
grep = spawn('grep', ['ssh']);
grep.on('exit', function (code, signal) {
console.log('child process terminated due to receipt of signal '+signal);
});
// send SIGHUP to process
grep.kill('SIGHUP');

注意,雖然此函數名為kill,但發(fā)送的信號不一定終止子進程。kill 實際上只會向進程發(fā)送信號。 See kill(2) 更多信息請參考kill(2)


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號