Timers 計時器

2022-05-16 09:08 更新

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

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

clearTimeout(timeoutId)

清除指定的定時。

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

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

.end(); clearInterval(intervalId)

清除指定的重復(fù)延時回調(diào)。

Child Processes 子進(jìn)程

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

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

Event: 'exit'

function (code, signal) {}

此事件在子進(jìn)程結(jié)束后被觸發(fā)。如果進(jìn)程正常結(jié)束,code 參數(shù)的值就是子進(jìn)程退出后的返回值,否則此參數(shù)為 null。如果進(jìn)程因?yàn)樾盘柖K止,參數(shù)signal 就是信號的名稱,否則此參數(shù)為null。 觸發(fā)此事件之后,node 將不再觸發(fā)'output'和'error'事件。 See waitpid(2).

child.stdin

可寫流(Writable Stream),代表子進(jìn)程的標(biāo)準(zhǔn)輸入(stdin)。使用end()函數(shù)關(guān)閉此流(stream),通常會終止子進(jìn)程。

child.stdout

只讀流(Readable Stream),代表子進(jìn)程的標(biāo)準(zhǔn)輸出(stdout)。 child.stderr

只讀流(Readable Stream),代表子進(jìn)程的標(biāo)準(zhǔn)錯誤輸出(stderr)。

child.pid

子進(jìn)程的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])

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

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

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

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

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);
});

例子: 實(shí)現(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)

使用子進(jìn)程執(zhí)行命令,緩存子進(jìn)程的輸出,并將子進(jìn)程的輸出以回調(diào)函數(shù)參數(shù)的形式返回。

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);
}
});

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

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

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

child.kill(signal='SIGTERM')

向子進(jìn)程發(fā)送信號。如果不指定參數(shù),默認(rèn)發(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');

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號