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');
});
}
由于在我翻译的时候 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');
});
}
相关文章推荐
- Node.js中child_process实现多进程
- Windows下Node.js使用child_process模块调用系统命令的编码问题
- 从Node.js的child_process模块来学习父子进程之间的通信
- 详解从Node.js的child_process模块来学习父子进程之间的通信
- Node.js中的child_process模块详解
- Node.js使用child_process运行脚本的输入输出错误
- Node.js进程通信模块child_process
- The Node.js Event Loop, Timers, and process.nextTick()
- nw.js node-webkit系列(25)child_process中spawn和exec方法的使用
- Node.js 入门笔记(5) - child_process
- linux 下node.js 使用child_process模块的exec方法所遇问题
- Node.js进程通信模块child_process
- Under the Hood: Hadoop Distributed Filesystem reliability with Namenode and Avatarnode
- Node.js进程通信模块child_process
- Node.js中child_process实现多进程
- phantomjs - node.js parse child process output line by line (spawn) - Stack Overflow
- Getting Started with Mongoose and Node.js – A Sample Comments System | Dev Notes
- Node.js 中 child_process模块的spawn和exec 方法之间的区别
- Node.js 使用 child_process 实现多进程
- nodejs File System 文件系统操作函数分类