您的位置:首页 > Web前端 > Node.js

Node.JS 5.ChildProcessAndFileSystem

2011-09-27 15:35 323 查看
ChildProcess

由于在我翻译的时候 Node 的 API 已经改了许多,所以这里是更改后的内容。

Node 在 ChildProcess 中提供了一种三向的 popen(3) 方法,他们分别是 stdin, stdout, stderr,他们都是使用流数据的格式以无阻塞(non-blocking) 的方式来在进程中传递数据的。 有一点你必须时刻记在心的是,使用异步方式的时候,所有操作的时序性都是不能预料的。

以下是个简单的示例

var sys = require('sys'),
child_process = require('child_process'),
filename = process.argv[2];

if(!filename){
return sys.puts('Usage : node ' + __filename.replace(__dirname + '/', '') + 'filename');

var tail = child_process.spawn('tail', ['-f', filename]);

tail.stdout.on('data', function(data){
sys.debug(data);
});

tail.on('exit', function(code){
sys.debug(code);
});

setTimeout(function(){
tail.kill();
}, 10000);

在这里首先创建了一个 tail 的子进程,亦即 spawn, 然后在他的标准输出上监听,

当 tail 子进程往标准输出写入任何东西的时候就会调用回调函数,当然这里我们做的只是

以 debug 方式输出。

接着对 tail 的退出也增加了监控,输出他的状态码。

最后是设置了一个10秒的超时,在超时之后结束 tail 进程。

File System

File I/O 提供了对标准 POSIX 函数的简单包装,要使用这些只需要使用 require('fs'),

而所有的这些方法都提供了同步跟异步两种方式。

接下来的示例将要做的是: 1. 创建一个新的名为 test_file.txt 的文件 2.往文件中写入数据

3.对文件添加监控,以跟踪他的变化,然后改变他的访问权限 4.文件跟踪的代码将会显示

该文件的变化。 5. 输出当前文件夹的内容. 6.输出 test_file.txt 的内容 7.停止文件监控

8.删除该文件

1.创建文件

var file = path.join(__dirname, 'test_file.txt');

fs.open(file, 'w', 0644, function(err, fd){
if(err) throw err;
});

2.往文件中写入数据。 fs.write 方法可以往文件中写入数据,但真正把数据写入磁盘的话,

需要调用同步的关闭方法 fs.closeSync

fs.write(fd, 'Hello World', 0, 'utf8', function(err, written){
if(err) throw err;
fs.closeSync(fd);
});

3.为文件添加监控,fs.watchFile提供了一个回调绑定,允许在文件属性发生变化的时候触发回调函数,

并做相应处理。

fs.watchFile(file, function(curr, prev){
sys.puts('\n\ttest_file.txt has been edited');
sys.puts('\tthe current mtime is :' + curr.mtime);
sys.puts('\tthe previous mtime was: '+ prev.mtime + '\n');
});

4.改变文件的访问权限

fs.chmod(file, 0777, function(err){
if(err) throw err;
});

5.输出当前目录内容

fs.readdir(__dirname, function(err, files){
if(err) throw err;

sys.puts(sys.inspect(files);
show_file_content();
});

6.输出 test_file.txt 的内容

fs.readFile(file, function(err, content){
if(err) throw err;
sys.puts(content);
});

7.停止对文件的监控

fs.unwatchFile(file);

8.删除文件

fs.unlink(file, function(err){
if(err) throw err;
});

好了,接下来是完整的实例

var sys = require('sys'),
path = require('path'),
fs = require('fs');

var file = path.join(__dirname, 'test_file.txt');

fs.open(file, 'w', 0644, function(err, fd){
if(err) throw err;

sys.puts('File test_file.txt opened');

fs.write(fd, 'Hello World', 0, 'utf8', function(err, written){
sys.puts('Data written');
if(err) throw err;

fs.closeSync(fd);

fs.watchFile(file, function(curr, prev){
sys.puts('\n\ttest_file.txt has been edited');
sys.puts('\tthe current mtime is: ' + curr.mtime);
sys.puts('\tthe previous mtime was: ' + prev.mtim);
});

chmod_file();
});
});

function chmod_file(){
fs.chmod(file, 0777, function(err){
if(err) throw err;

sys.puts('\nchmod value of test_file.txt set to 777');

fs.chmodSync(file, 0644);
sys.puts('chomd value of test_file.txt set to 0644');

show_dir();
});
}

function show_dir(){
fs.readdir(__dirname, function(err, files){
if(err) throw err;

sys.puts(sys.inspect(files));
show_file_content();
});
}

function show_file_content(){
fs.readFile(file, function(err, content){
if(err) throw err;

sys.puts('\nContent of test_file.txt:');
sys.puts(content);
delete_file();
});
}

function delete_file(){
fs.unwatchFile(file);
sys.puts('\nStoping watchfile of test_file.txt');

fs.unlink(file, function(err){
if(err) throw err;
sys.puts('\ntest_file.txt has been deleted');
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: