gdb如何调试子进程玩转子进程child

Nodejs进阶:如何玩转子进程(child_process)_百度文库
您的浏览器Javascript被禁用,需开启后体验完整功能,
享专业文档下载特权
&赠共享文档下载特权
&10W篇文档免费专享
&每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Nodejs进阶:如何玩转子进程(child_process)
&&Nodejs进阶:如何玩转子进程(child_process)
阅读已结束,下载本文需要
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
加入VIP
还剩11页未读,
定制HR最喜欢的简历
你可能喜欢新的地址:dongss.cn
Node.js 使用 child_process 实现多进程
nodejs是一种单线程模型,但是,使用nodejs的child_process模块可以实现多进程任务。利用child_process可以创建子进程,实现子进程和主进程之间的通信。
nodejs v0.12.7版本child_process提供以下同步和异步的方式创建进程:
异步创建:
.spawn([, args][, options])
.customFds
.exec([, options], )
.execFile([, args][, options][callback])
.fork([, args][, options])
同步创建:
.spawnSync([, args][, options])
.execFileSync([, args][, options])
.execSync([, options])
各函数及参数文档可见
spawn 函数
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function(data) {
console.log('stdout: ' + data);
ls.stderr.on('data', function(data) {
console.log('stderr: ' + data);
ls.on('close', function(code) {
console.log('child process exited with code ' + code);
-bash-4.1$ node child_process.js
stdout: total 76K
dr-xr-xr-x.
2 root root
20K Jul 23 11:15 bin
drwxr-xr-x.
2 root root 4.0K Sep 23
drwxr-xr-x.
2 root root 4.0K Sep 23
2011 games
drwxr-xr-x. 53 root root 4.0K Nov 29
2014 include
dr-xr-xr-x. 26 root root 4.0K Nov 19
dr-xr-xr-x. 35 root root
20K Nov 29
2014 lib64
drwxr-xr-x. 12 root root 4.0K Nov 21
2014 libexec
drwxr-xr-x. 13 root root 4.0K Jan
2015 local
dr-xr-xr-x.
2 root root 4.0K Jun 16 15:33 sbin
drwxr-xr-x. 96 root root 4.0K May
7 09:48 share
drwxr-xr-x.
4 root root 4.0K Mar 27
lrwxrwxrwx.
1 root root
2013 tmp -& ../var/tmp
child process exited with code 0
例 2 花式实现ps ax|grep child
var spawn = require('child_process').spawn,
ps = spawn('ps', ['ax']),
grep = spawn('grep', ['child']);
ps.stdout.on('data', function(data) {
grep.stdin.write(data);
ps.stderr.on('data', function(data) {
console.log('ps stderr: ' + data);
ps.on('close', function(code) {
if (code !== 0) {
console.log('ps process exited with code ' + code);
grep.stdin.end();
grep.stdout.on('data', function(data) {
console.log('' + data);
grep.stderr.on('data', function(data) {
console.log('grep stderr: ' + data);
grep.on('close', function(code) {
if (code !== 0) {
console.log('grep process exited with code ' + code);
-bash-4.1$ node child_process.js
9940 pts/4
0:00 node child_process.js
12876 pts/4
0:00 vim child_process.js
30870 pts/4
0:00 vim child_process.js
例 1 统计文件字数,3列分别为 行数、字数、字节数
var exec = require('child_process').exec,
child = exec('cat child_process.js | wc', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
-bash-4.1$ node child_process.js
exec函数第二个可选参数默认值:
{ encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null }
exec函数不安全,使用用户输入作为参数时不应使用
execFile 函数
var childProcess = require('child_process');
childProcess.execFile('/bin/ls', ['-l', '.'], function(err, result) );
-bash-4.1$ node child_process.js
-rw-rw-r-- 1 dongshaoshuai dongshaoshuai 144 Jul 31 17:02 child_process.js
使用execFile安全防注入
fork 可以实现父进程和子进程的通信
var childProcess = require('child_process');
var child = childProcess.fork('./child.js');
child.on('message', function(msg) {
console.log("Main on message: ", msg);
child.send({message: "I love you !"});
process.on('message', function(m) {
console.log('Child listen:', m);
process.send({ message: 'You love me'});
运行main.js
: { message: 'I love you !' }
{ message: 'You love me' }
没有更多推荐了,Node Expressjs 里,创建子进程( child_process.fork)报错
16:04:22 +08:00 · 2884 次点击
Node Expressjs 里面,用子进程( child_process.fork )执行 js 就会报错,错误信息如下:
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11 )
at Agent.Server._listen2 (net.js:1156:14 )
at listen (net.js:1182:10 )
at Agent.Server.listen (net.js:1267:5 )
at Object.start (_debugger_agent.js:20:9 )
at startup (node.js:86:9 )
at node.js:814:3
POST: process.send ('123');
var code = req.body.toString ('utf8');
var crawlerPath = __dirname+'/some/path/test';
fs.writeFileSync (crawlerPath+'.js',code,'utf8');
var crawlerProcess = cp.fork (crawlerPath );
6 回复 &| &直到
20:55:35 +08:00
& & 16:22:25 +08:00
EADDRINUSE: 给定的地址已经被使用
不需要说什么了
& & 16:46:48 +08:00
@ 我知道是端口被占用,但是不知道为什么会被占用
用 cp.exec ('node '+crawlerPath )没有问题,也可以正确执行,但是 fork 不行,如果需要设置端口,具体是什么参数呢?我在文档里面没有找到详细的说明
& & 16:58:52 +08:00
@ 好吧。。。刚才发现问题了,我在 WebStorm 里面调试的时候就会报错,正常运行的时候就正常了,估计还是 debug 的原因吧
& & 12:33:00 +08:00
您好。。问下这个问题你后来是怎么解决的
& & 16:12:41 +08:00
@ 额,是 webstorm debug 的问题,好像和代码没啥关系。直接 run 不会报错
& & 20:55:35 +08:00
@ 嗯。。。我这边的话是用了公司的一个框架的问题
& · & 1090 人在线 & 最高记录 3541 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.1 · 16ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18? Do have faith in what you're doing.使用child_process()创建多个子进程,将总控制台中不同子进程的信息分发给指定用户? - 知乎有问题,上知乎。知乎作为中文互联网最大的知识分享平台,以「知识连接一切」为愿景,致力于构建一个人人都可以便捷接入的知识分享网络,让人们便捷地与世界分享知识、经验和见解,发现更大的世界。9被浏览329分享邀请回答en.wikipedia.org/wiki/Inter-process_communication标准流是最常用的一种, Node.js 的标准流 API 还是很方便很强大的。因为不知道题主到底想做什么,只能建议题主多学习,懂得多了就不怕了。赞同 1 条评论分享收藏感谢收起赞同 添加评论分享收藏感谢收起写回答

我要回帖

更多关于 父进程如何创建子进程 的文章

 

随机推荐