如何派生一个侦听与父进程不同的调试端口的子进程 [英] How to fork a child process that listens on a different debug port than the parent

查看:113
本文介绍了如何派生一个侦听与父进程不同的调试端口的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用child_process.fork生成一个中断并侦听V8调试协议的进程.

I am trying to use child_process.fork to spawn a process that breaks and listens on the V8 debug protocol.

但是,我无法让派生进程监听与父进程不同的端口.假设父进程在6000上侦听,子进程也尝试在端口6000上侦听:

However, I can't get the forked process to listen on a port that's different from the parent process. Assuming the parent process listens on 6000, the child process also attempts to listen on port 6000:

无法打开端口6000上的套接字,请等待1000毫秒,然后重试

Failed to open socket on port 6000, waiting 1000 ms before retrying

这是我的代码:

// `test.js`, invoked as `node --debug-brk=6000 test.js`

var nodeModule, args, env, child

nodeModule = path.normalize(path.join(__dirname, '..', 'app.js'))

args = [
    '--debug-brk=6001'
  , '127.0.0.1'
  , 3030
  , 'api-testing'
]

env = { 'DB_URI': 'mongodb://localhost/test' }

child = require('child_process')
  .fork(nodeModule, args, {env: env})
  .on('message', callback)

如您所见,我正在尝试让分叉的进程在端口6001上进行侦听,但是子进程尝试在父级正在使用的端口6000上进行侦听.

As you can see, I'm trying to get the forked process to listen on port 6001, but the child process attempts to listen on port 6000 which is in use by the parent.

如何让子进程在端口6001或其他一些空闲端口上侦听?

How can I get the child process to listen on port 6001, or some other free port?

关于此主题有多个主题.例如:

There are several threads on this subject. For example:

  • How to debug Node.JS child forked process?
  • Debugging Node.js processes with cluster.fork()

但是:

  • These threads deal with the cluster variant of forking
  • Refer to execArgv, which appear to have been undocumented for process and are still undocumented for cluster.

推荐答案

足够简单的答案,可在

Simple enough answer, found on this comment and with some help from #Node.js on Freenode:

只需将--debug-brk移至options参数的execArgv键中,然后将其移至fork:

Just move the --debug-brk into the execArgv key of the options param to fork:

// Excerpt:

args = [
   '127.0.0.1'
  , 3030
  , 'api-testing'
]

env = { 'DB_URI': 'mongodb://localhost/test' }

child = fork(nodeModule, args, {
    env: env
  , execArgv: ['--debug-brk=6001']
})
  .on('message', this.callback)

execArgv是传递给节点进程的参数数组. argv是传递到主模块的集合.对于argvchild_process.fork有一个专用参数,但是execArgv必须放置在opts参数中.这行得通,在子进程中,我们有:

execArgv is the array of parameters passed to the node process. argv is the set passed to the main module. There's a dedicated parameter to child_process.fork for argv, but execArgvs have to be placed within the opts param. This works, and in the child process we have:

> process.execArgv 
["--debug-brk=6001"]
> process.argv
["/usr/local/Cellar/node/0.10.13/bin/node" "/Users/dmitry/dev/linksmotif/app.js", "127.0.0.1", "3030", "api-testing"] 

总结

Node.js始终将execArgvargv视为单独的值集.

Node.js consistently treats execArgv and argv as separate sets of values.

这篇关于如何派生一个侦听与父进程不同的调试端口的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆